import { DictionaryEntry } from "@open-mercato/core/modules/dictionaries/data/entities";
import { registerDictionaryEntryCommands } from "@open-mercato/core/modules/dictionaries/commands/factory";
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import {
  ensureSalesDictionary,
  getSalesDictionaryDefinition
} from "../lib/dictionaries.js";
import {
  ensureOrganizationScope,
  ensureTenantScope
} from "./shared.js";
import {
  statusDictionaryCreateSchema,
  statusDictionaryUpdateSchema
} from "../data/validators.js";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
const commandIds = [
  "sales.order-statuses.create",
  "sales.order-statuses.update",
  "sales.order-statuses.delete",
  "sales.order-line-statuses.create",
  "sales.order-line-statuses.update",
  "sales.order-line-statuses.delete",
  "sales.shipment-statuses.create",
  "sales.shipment-statuses.update",
  "sales.shipment-statuses.delete",
  "sales.payment-statuses.create",
  "sales.payment-statuses.update",
  "sales.payment-statuses.delete",
  "sales.adjustment-kinds.create",
  "sales.adjustment-kinds.update",
  "sales.adjustment-kinds.delete"
];
function ensureScope(ctx, scope) {
  ensureTenantScope(ctx, scope.tenantId);
  ensureOrganizationScope(ctx, scope.organizationId);
}
function registerStatusDictionaryCommands(kind) {
  const definition = getSalesDictionaryDefinition(kind);
  registerDictionaryEntryCommands({
    commandPrefix: definition.commandPrefix,
    resourceKind: definition.resourceKind,
    translationKeyPrefix: definition.commandPrefix,
    createSchema: statusDictionaryCreateSchema,
    updateSchema: statusDictionaryUpdateSchema,
    ensureScope,
    duplicateError: "Value already exists in this dictionary.",
    labels: {
      singular: definition.singular,
      create: `Create ${definition.singular}`,
      update: `Update ${definition.singular}`,
      delete: `Delete ${definition.singular}`
    },
    resolveDictionaryForCreate: async ({ em, ctx, parsed }) => {
      const dictionary = await ensureSalesDictionary({
        em,
        tenantId: parsed.tenantId,
        organizationId: parsed.organizationId,
        kind
      });
      const scope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId };
      ensureScope(ctx, scope);
      return { dictionary, scope };
    },
    resolveEntry: async ({ em, ctx, id }) => {
      const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ["dictionary"] });
      if (!entry) {
        throw new CrudHttpError(404, { error: "Dictionary entry not found" });
      }
      if (entry.dictionary.key !== definition.key) {
        throw new CrudHttpError(400, { error: "Entry does not belong to this dictionary." });
      }
      const scope = { tenantId: entry.tenantId, organizationId: entry.organizationId };
      ensureScope(ctx, scope);
      return { entry, dictionary: entry.dictionary, scope };
    },
    ensureDictionaryForUndo: async ({ em, snapshot }) => {
      return ensureSalesDictionary({
        em,
        tenantId: snapshot.tenantId,
        organizationId: snapshot.organizationId,
        kind
      });
    }
  });
}
registerStatusDictionaryCommands("order-status");
registerStatusDictionaryCommands("order-line-status");
registerStatusDictionaryCommands("shipment-status");
registerStatusDictionaryCommands("payment-status");
registerStatusDictionaryCommands("adjustment-kind");
export {
  commandIds
};
//# sourceMappingURL=statuses.js.map
