import {
  INTEGRATION_MULTI_TENANT
} from "./chunk-S7C6TIIF.js";
import {
  assertTenantId
} from "./chunk-MZ6GV4YF.js";
import {
  integrationSubscriptions
} from "./chunk-HNWZFNKP.js";
import {
  DRIZZLE
} from "./chunk-U64T4YZE.js";
import {
  __decorateClass,
  __decorateParam
} from "./chunk-2E224ZSN.js";

// runtime/subsystems/integration/integration-cursor-store.drizzle-backend.ts
import { Inject, Injectable, Optional } from "@nestjs/common";
import { and, desc, eq } from "drizzle-orm";
var PostgresCursorStore = class {
  constructor(db, multiTenant) {
    this.db = db;
    this.multiTenant = multiTenant ?? false;
  }
  db;
  multiTenant;
  async get(subscriptionId, tenantId) {
    const where = this.buildWhere(subscriptionId, tenantId, "cursor.get");
    const rows = await this.db.select({ cursor: integrationSubscriptions.cursor }).from(integrationSubscriptions).where(where).limit(1);
    if (rows.length === 0) return null;
    return rows[0]?.cursor ?? null;
  }
  async put(subscriptionId, cursor, tenantId) {
    const where = this.buildWhere(subscriptionId, tenantId, "cursor.put");
    await this.db.update(integrationSubscriptions).set({
      cursor,
      lastIntegrationAt: /* @__PURE__ */ new Date(),
      updatedAt: /* @__PURE__ */ new Date()
    }).where(where);
  }
  async listAll(tenantId) {
    assertTenantId(tenantId, {
      multiTenant: this.multiTenant,
      operation: "cursor.listAll"
    });
    const where = this.multiTenant ? eq(integrationSubscriptions.tenantId, tenantId) : void 0;
    const rows = await this.db.select({
      id: integrationSubscriptions.id,
      connectionId: integrationSubscriptions.connectionId,
      adapter: integrationSubscriptions.adapter,
      domain: integrationSubscriptions.domain,
      externalRef: integrationSubscriptions.externalRef,
      cursor: integrationSubscriptions.cursor,
      lastIntegrationAt: integrationSubscriptions.lastIntegrationAt,
      updatedAt: integrationSubscriptions.updatedAt,
      tenantId: integrationSubscriptions.tenantId
    }).from(integrationSubscriptions).where(where).orderBy(desc(integrationSubscriptions.updatedAt));
    return rows.map((row) => ({
      subscriptionId: row.id,
      connectionId: row.connectionId,
      adapter: row.adapter,
      domain: row.domain,
      externalRef: row.externalRef,
      cursor: row.cursor ?? null,
      lastIntegrationAt: row.lastIntegrationAt,
      updatedAt: row.updatedAt,
      tenantId: row.tenantId
    }));
  }
  /**
   * Centralized WHERE clause — `get` and `put` share identical semantics.
   * Drift here would let a caller read under one tenancy rule and write
   * under another.
   */
  buildWhere(subscriptionId, tenantId, operation) {
    assertTenantId(tenantId, {
      multiTenant: this.multiTenant,
      operation
    });
    if (this.multiTenant) {
      return and(
        eq(integrationSubscriptions.id, subscriptionId),
        eq(integrationSubscriptions.tenantId, tenantId)
      );
    }
    return eq(integrationSubscriptions.id, subscriptionId);
  }
};
PostgresCursorStore = __decorateClass([
  Injectable(),
  __decorateParam(0, Inject(DRIZZLE)),
  __decorateParam(1, Optional()),
  __decorateParam(1, Inject(INTEGRATION_MULTI_TENANT))
], PostgresCursorStore);

export {
  PostgresCursorStore
};
//# sourceMappingURL=chunk-XWBK3XJK.js.map