import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { backfillDealLossReasonDictionary, seedSalesAdjustmentKinds, seedSalesStatusDictionaries } from "./lib/dictionaries.js";
import { seedSalesTaxRates } from "./lib/seeds.js";
import { ensureExamplePaymentMethods, ensureExampleShippingMethods } from "./seed/examples-data.js";
import { seedSalesExamples } from "./seed/examples.js";
function parseArgs(rest) {
  const args = {};
  for (let i = 0; i < rest.length; i += 1) {
    const part = rest[i];
    if (!part) continue;
    if (part.startsWith("--")) {
      const [rawKey, rawValue] = part.slice(2).split("=");
      if (rawValue !== void 0) args[rawKey] = rawValue;
      else if (rest[i + 1] && !rest[i + 1].startsWith("--")) {
        args[rawKey] = rest[i + 1];
        i += 1;
      }
    }
  }
  return args;
}
const seedTaxRatesCommand = {
  command: "seed-tax-rates",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-tax-rates --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const em = container.resolve("em");
    try {
      await em.transactional(async (tem) => {
        await seedSalesTaxRates(tem, { tenantId, organizationId });
      });
      console.log("\u{1F9FE} Tax rates seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedStatusesCommand = {
  command: "seed-statuses",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-statuses --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedSalesStatusDictionaries(tem, { tenantId, organizationId });
        await tem.flush();
      });
      console.log("\u{1F6A6} Sales order statuses seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedAdjustmentKindsCommand = {
  command: "seed-adjustment-kinds",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-adjustment-kinds --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedSalesAdjustmentKinds(tem, { tenantId, organizationId });
        await tem.flush();
      });
      console.log("\u2699\uFE0F  Sales adjustment kinds seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const backfillDealLossReasonsCommand = {
  command: "backfill-deal-loss-reasons",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales backfill-deal-loss-reasons --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      const result = await em.transactional(async (tem) => {
        const backfillResult = await backfillDealLossReasonDictionary(tem, { tenantId, organizationId });
        await tem.flush();
        return backfillResult;
      });
      console.log(
        [
          `Deal loss reasons dictionary ready for organization ${organizationId}.`,
          `dictionaryId=${result.dictionaryId}`,
          `createdDictionary=${String(result.createdDictionary)}`,
          `copiedLegacyEntries=${String(result.copiedLegacyEntries)}`,
          `seededDefaultEntries=${String(result.seededDefaultEntries)}`,
          `existingEntries=${String(result.existingEntries)}`
        ].join(" ")
      );
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedShippingMethodsCommand = {
  command: "seed-shipping-methods",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-shipping-methods --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await ensureExampleShippingMethods(tem, { tenantId, organizationId });
      });
      console.log("\u{1F69A} Shipping methods seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedPaymentMethodsCommand = {
  command: "seed-payment-methods",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-payment-methods --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await ensureExamplePaymentMethods(tem, { tenantId, organizationId });
      });
      console.log("\u{1F4B3} Payment methods seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedExamplesCommand = {
  command: "seed-examples",
  async run(rest) {
    const args = parseArgs(rest);
    const tenantId = String(args.tenantId ?? args.tenant ?? "");
    const organizationId = String(args.organizationId ?? args.org ?? args.orgId ?? "");
    if (!tenantId || !organizationId) {
      console.error("Usage: mercato sales seed-examples --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    try {
      const em = container.resolve("em");
      const seeded = await em.transactional(
        async (tem) => seedSalesExamples(tem, container, { tenantId, organizationId })
      );
      if (seeded) {
        console.log("\u{1F9FE} Sales example quotes and orders seeded for organization", organizationId);
      } else {
        console.log("Sales example data already present; skipping");
      }
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
var cli_default = [
  seedTaxRatesCommand,
  seedStatusesCommand,
  seedAdjustmentKindsCommand,
  backfillDealLossReasonsCommand,
  seedShippingMethodsCommand,
  seedPaymentMethodsCommand,
  seedExamplesCommand
];
export {
  cli_default as default
};
//# sourceMappingURL=cli.js.map
