import { env } from "cloudflare:workers";
import { first } from "../db";
import { accountDb, stmt } from "../scoped-db";
import type { ScopedStatement } from "../scoped-db";
import { meterDef } from "~/lib/meters";
import type { MeterId } from "~/lib/meters";
import { isPlanId } from "~/lib/plans";
import { creditPoolFor } from "~/lib/pricing";
import { randomId } from "../crypto";
import { HTTP_PAYMENT_REQUIRED, HttpError } from "../errors";
import type { PaymentReason } from "../errors";
import { otherHoldsSql } from "./credits";
import { billingSettings, hardCap } from "./settings";
import { holdGauge } from "./gauge-hold";
import { noteScratchAccount } from "./directory";
import type { BillingSettings } from "./settings";
export { billingSettings, hardCap, plans } from "./settings";
export type { BillingSettings } from "./settings";
import { spendGuard } from "./spend-guard";
import { paymentErrorFor } from "./access";
import {
eventStatements,
MeteringError,
periodEnd,
periodOf,
resolveAccount,
validate,
writeAnalytics,
} from "./record";
import type { UsageInput } from "./record";
import { forgetSupporter } from "../supporter";
const DEFAULT_TTL_MS = 3_600_000;
/**
* Out of budget. Deliberately not an HttpError, so existing handlers keep their behaviour (background work
* skips, integrations retry); request boundaries turn it into a 402 with toHttpError().
*/
export class QuotaExceededError extends Error {