import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import {
  installExampleCatalogData,
  seedCatalogExamplesForScope,
  seedCatalogPriceKinds,
  seedCatalogUnits
} from "./lib/seeds.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 seedUnitsCommand = {
  command: "seed-units",
  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 catalog seed-units --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedCatalogUnits(tem, scope);
      });
      console.log("\u{1F4CF} Unit dictionary seeded for organization", organizationId);
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
const seedPriceKindsCommand = {
  command: "seed-price-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) {
      console.error("Usage: mercato catalog seed-price-kinds --tenant <tenantId> [--org <organizationId>]");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId: organizationId || null };
    try {
      const em = container.resolve("em");
      await em.transactional(async (tem) => {
        await seedCatalogPriceKinds(tem, scope);
      });
      console.log(
        "\u{1F3F7}\uFE0F Price kinds seeded for tenant",
        tenantId,
        organizationId ? `(org: ${organizationId})` : "(org: shared)"
      );
    } 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 catalog seed-examples --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    let seeded = false;
    try {
      const em = container.resolve("em");
      seeded = await em.transactional(
        async (tem) => seedCatalogExamplesForScope(tem, container, scope)
      );
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
    if (seeded) {
      console.log("Catalog example data seeded for organization", organizationId);
    } else {
      console.log("Catalog example data already present; skipping");
    }
  }
};
const installExamplesBundle = {
  command: "seed-examples-bundle",
  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 catalog seed-examples-bundle --tenant <tenantId> --org <organizationId>");
      return;
    }
    const container = await createRequestContainer();
    const scope = { tenantId, organizationId };
    try {
      const em = container.resolve("em");
      const { seededExamples } = await em.transactional(
        async (tem) => installExampleCatalogData(container, scope, tem)
      );
      if (seededExamples) {
        console.log("Catalog example data seeded for organization", organizationId);
      } else {
        console.log("Catalog example data already present; skipping examples");
      }
    } finally {
      const disposable = container;
      if (typeof disposable.dispose === "function") {
        await disposable.dispose();
      }
    }
  }
};
var cli_default = [seedUnitsCommand, seedPriceKindsCommand, seedExamplesCommand, installExamplesBundle];
export {
  cli_default as default
};
//# sourceMappingURL=cli.js.map
