import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { getIntegration } from "@open-mercato/shared/modules/integrations/types";
import { IntegrationState } from "../data/entities.js";
function createIntegrationStateService(em) {
  function resolveDefinitionDefaults(integrationId) {
    const definition = getIntegration(integrationId);
    return {
      isEnabled: definition?.defaultState?.isEnabled ?? false,
      apiVersion: null,
      reauthRequired: false,
      lastHealthStatus: null,
      lastHealthCheckedAt: null,
      lastHealthLatencyMs: null,
      enabledAt: null,
      updatedAt: null
    };
  }
  return {
    async get(integrationId, scope) {
      return findOneWithDecryption(
        em,
        IntegrationState,
        {
          integrationId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
    },
    async resolveState(integrationId, scope) {
      const state = await this.get(integrationId, scope);
      const defaults = resolveDefinitionDefaults(integrationId);
      return {
        isEnabled: state?.isEnabled ?? defaults.isEnabled,
        apiVersion: state?.apiVersion ?? null,
        reauthRequired: state?.reauthRequired ?? false,
        lastHealthStatus: state?.lastHealthStatus ?? null,
        lastHealthCheckedAt: state?.lastHealthCheckedAt ?? null,
        lastHealthLatencyMs: state?.lastHealthLatencyMs ?? null,
        enabledAt: state?.enabledAt ?? null,
        updatedAt: state?.updatedAt ?? null
      };
    },
    async isEnabled(integrationId, scope) {
      const state = await this.resolveState(integrationId, scope);
      return state.isEnabled;
    },
    async upsert(integrationId, input, scope) {
      const current = await this.get(integrationId, scope);
      const resolvedBefore = await this.resolveState(integrationId, scope);
      const enableTransition = input.isEnabled === true && resolvedBefore.isEnabled === false;
      if (current) {
        if (input.isEnabled !== void 0) current.isEnabled = input.isEnabled;
        if (input.apiVersion !== void 0) current.apiVersion = input.apiVersion;
        if (input.reauthRequired !== void 0) current.reauthRequired = input.reauthRequired;
        if (input.lastHealthStatus !== void 0) current.lastHealthStatus = input.lastHealthStatus;
        if (input.lastHealthCheckedAt !== void 0) current.lastHealthCheckedAt = input.lastHealthCheckedAt;
        if (input.lastHealthLatencyMs !== void 0) current.lastHealthLatencyMs = input.lastHealthLatencyMs;
        if (input.enabledAt !== void 0) current.enabledAt = input.enabledAt;
        else if (enableTransition) current.enabledAt = /* @__PURE__ */ new Date();
        await em.flush();
        return current;
      }
      const created = em.create(IntegrationState, {
        integrationId,
        isEnabled: input.isEnabled ?? false,
        apiVersion: input.apiVersion,
        reauthRequired: input.reauthRequired ?? false,
        lastHealthStatus: input.lastHealthStatus,
        lastHealthCheckedAt: input.lastHealthCheckedAt,
        lastHealthLatencyMs: input.lastHealthLatencyMs,
        enabledAt: input.enabledAt !== void 0 ? input.enabledAt : enableTransition ? /* @__PURE__ */ new Date() : void 0,
        organizationId: scope.organizationId,
        tenantId: scope.tenantId
      });
      await em.persist(created).flush();
      return created;
    },
    async resolveApiVersion(integrationId, scope) {
      const state = await this.get(integrationId, scope);
      return state?.apiVersion ?? void 0;
    },
    async setReauthRequired(integrationId, required, scope) {
      return this.upsert(integrationId, { reauthRequired: required }, scope);
    }
  };
}
export {
  createIntegrationStateService
};
//# sourceMappingURL=state-service.js.map
