import { registerCommand } from "@open-mercato/shared/lib/commands";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { CustomerSettings } from "../data/entities.js";
import {
  customerDictionarySortModesUpsertSchema,
  customerSettingsUpsertSchema,
  customerStuckThresholdUpsertSchema
} from "../data/validators.js";
import { ensureOrganizationScope, ensureTenantScope } from "./shared.js";
async function loadCustomerSettings(em, params) {
  return findOneWithDecryption(
    em,
    CustomerSettings,
    {
      tenantId: params.tenantId,
      organizationId: params.organizationId
    },
    void 0,
    { tenantId: params.tenantId, organizationId: params.organizationId }
  );
}
const saveCustomerSettingsCommand = {
  id: "customers.settings.save",
  async execute(rawInput, ctx) {
    const input = customerSettingsUpsertSchema.parse(rawInput);
    ensureTenantScope(ctx, input.tenantId);
    ensureOrganizationScope(ctx, input.organizationId);
    const em = ctx.container.resolve("em").fork();
    let settings = await loadCustomerSettings(em, {
      tenantId: input.tenantId,
      organizationId: input.organizationId
    });
    if (!settings) {
      settings = em.create(CustomerSettings, {
        tenantId: input.tenantId,
        organizationId: input.organizationId,
        addressFormat: input.addressFormat
      });
      em.persist(settings);
    } else if (settings.addressFormat !== input.addressFormat) {
      settings.addressFormat = input.addressFormat;
      settings.updatedAt = /* @__PURE__ */ new Date();
    }
    await em.flush();
    return {
      settingsId: settings.id,
      addressFormat: settings.addressFormat
    };
  }
};
registerCommand(saveCustomerSettingsCommand);
const saveStuckThresholdCommand = {
  id: "customers.settings.save_stuck_threshold",
  async execute(rawInput, ctx) {
    const input = customerStuckThresholdUpsertSchema.parse(rawInput);
    ensureTenantScope(ctx, input.tenantId);
    ensureOrganizationScope(ctx, input.organizationId);
    const em = ctx.container.resolve("em").fork();
    let settings = await loadCustomerSettings(em, {
      tenantId: input.tenantId,
      organizationId: input.organizationId
    });
    if (!settings) {
      settings = em.create(CustomerSettings, {
        tenantId: input.tenantId,
        organizationId: input.organizationId,
        stuckThresholdDays: input.stuckThresholdDays
      });
      em.persist(settings);
    } else if (settings.stuckThresholdDays !== input.stuckThresholdDays) {
      settings.stuckThresholdDays = input.stuckThresholdDays;
      settings.updatedAt = /* @__PURE__ */ new Date();
    }
    await em.flush();
    return {
      settingsId: settings.id,
      stuckThresholdDays: settings.stuckThresholdDays
    };
  }
};
registerCommand(saveStuckThresholdCommand);
const saveDictionarySortModesCommand = {
  id: "customers.settings.save_dictionary_sort_modes",
  async execute(rawInput, ctx) {
    const input = customerDictionarySortModesUpsertSchema.parse(rawInput);
    ensureTenantScope(ctx, input.tenantId);
    ensureOrganizationScope(ctx, input.organizationId);
    const em = ctx.container.resolve("em").fork();
    let settings = await loadCustomerSettings(em, {
      tenantId: input.tenantId,
      organizationId: input.organizationId
    });
    if (!settings) {
      settings = em.create(CustomerSettings, {
        tenantId: input.tenantId,
        organizationId: input.organizationId,
        dictionarySortModes: input.dictionarySortModes
      });
      em.persist(settings);
    } else {
      settings.dictionarySortModes = input.dictionarySortModes;
      settings.updatedAt = /* @__PURE__ */ new Date();
    }
    await em.flush();
    return {
      settingsId: settings.id,
      dictionarySortModes: settings.dictionarySortModes ?? {}
    };
  }
};
registerCommand(saveDictionarySortModesCommand);
export {
  loadCustomerSettings
};
//# sourceMappingURL=settings.js.map
