import { SalesSettings, SalesDocumentSequence, SalesTaxRate } from "./data/entities.js";
import { DEFAULT_ORDER_NUMBER_FORMAT, DEFAULT_QUOTE_NUMBER_FORMAT } from "./lib/documentNumberTokens.js";
import { seedSalesStatusDictionaries, seedSalesAdjustmentKinds } from "./lib/dictionaries.js";
import { ensureExampleShippingMethods, ensureExamplePaymentMethods } from "./seed/examples-data.js";
import { seedSalesExamples } from "./seed/examples.js";
const DEFAULT_TAX_RATES = [
  { code: "vat-23", name: "23% VAT", rate: "23" },
  { code: "vat-0", name: "0% VAT", rate: "0" }
];
async function seedSalesTaxRates(em, scope) {
  await em.transactional(async (tem) => {
    const existing = await tem.find(SalesTaxRate, {
      tenantId: scope.tenantId,
      organizationId: scope.organizationId,
      deletedAt: null
    });
    const existingCodes = new Set(existing.map((rate) => rate.code));
    const hasDefault = existing.some((rate) => rate.isDefault);
    const now = /* @__PURE__ */ new Date();
    let isFirst = !hasDefault;
    for (const seed of DEFAULT_TAX_RATES) {
      if (existingCodes.has(seed.code)) continue;
      tem.persist(
        tem.create(SalesTaxRate, {
          tenantId: scope.tenantId,
          organizationId: scope.organizationId,
          code: seed.code,
          name: seed.name,
          rate: seed.rate,
          priority: 0,
          isCompound: false,
          isDefault: isFirst,
          createdAt: now,
          updatedAt: now
        })
      );
      isFirst = false;
    }
  });
}
const setup = {
  defaultRoleFeatures: {
    admin: ["sales.*", "sales.documents.number.edit"],
    employee: [
      "sales.channels.view",
      "sales.channels.manage",
      "sales.settings.view",
      "sales.settings.manage",
      "sales.orders.view",
      "sales.orders.manage",
      "sales.orders.approve",
      "sales.widgets.new-orders",
      "sales.widgets.new-quotes",
      "sales.quotes.view",
      "sales.quotes.manage",
      "sales.shipments.manage",
      "sales.payments.manage",
      "sales.returns.view",
      "sales.returns.create",
      "sales.returns.manage",
      "sales.invoices.manage",
      "sales.credit_memos.manage"
    ]
  },
  async onTenantCreated({ em, tenantId, organizationId }) {
    const exists = await em.findOne(SalesSettings, { tenantId, organizationId });
    if (!exists) {
      em.persist(
        em.create(SalesSettings, {
          tenantId,
          organizationId,
          orderNumberFormat: DEFAULT_ORDER_NUMBER_FORMAT,
          quoteNumberFormat: DEFAULT_QUOTE_NUMBER_FORMAT,
          createdAt: /* @__PURE__ */ new Date(),
          updatedAt: /* @__PURE__ */ new Date()
        })
      );
    }
    for (const kind of ["order", "quote", "return", "invoice", "credit_memo"]) {
      const seq = await em.findOne(SalesDocumentSequence, {
        tenantId,
        organizationId,
        documentKind: kind
      });
      if (!seq) {
        em.persist(
          em.create(SalesDocumentSequence, {
            tenantId,
            organizationId,
            documentKind: kind,
            currentValue: 0,
            createdAt: /* @__PURE__ */ new Date(),
            updatedAt: /* @__PURE__ */ new Date()
          })
        );
      }
    }
    await em.flush();
  },
  async seedDefaults({ em, tenantId, organizationId }) {
    const scope = { tenantId, organizationId };
    await seedSalesTaxRates(em, scope);
    await seedSalesStatusDictionaries(em, scope);
    await seedSalesAdjustmentKinds(em, scope);
    await ensureExampleShippingMethods(em, scope);
    await ensureExamplePaymentMethods(em, scope);
  },
  async seedExamples({ em, container, tenantId, organizationId }) {
    const scope = { tenantId, organizationId };
    await seedSalesExamples(em, container, scope);
  }
};
var setup_default = setup;
export {
  setup_default as default,
  setup
};
//# sourceMappingURL=setup.js.map
