import { Organization } from "@open-mercato/core/modules/directory/data/entities";
import { slugify } from "@open-mercato/shared/lib/slugify";
async function backfillOrganizationSlugs(em, tenantId) {
  const filter = {
    tenant: tenantId,
    slug: null,
    deletedAt: null
  };
  const orgs = await em.find(Organization, filter);
  if (!orgs.length) return;
  const existingSlugs = /* @__PURE__ */ new Set();
  const allFilter = {
    tenant: tenantId,
    deletedAt: null
  };
  const allOrgs = await em.find(Organization, allFilter, { fields: ["slug"] });
  for (const org of allOrgs) {
    if (org.slug) existingSlugs.add(org.slug);
  }
  for (const org of orgs) {
    const base = slugify(org.name);
    if (!base) continue;
    let candidate = base;
    let suffix = 0;
    while (existingSlugs.has(candidate)) {
      suffix += 1;
      candidate = `${base}-${suffix}`;
    }
    org.slug = candidate;
    existingSlugs.add(candidate);
  }
  await em.flush();
}
const setup = {
  defaultRoleFeatures: {
    superadmin: ["directory.tenants.*"],
    admin: ["directory.organizations.view", "directory.organizations.manage"]
  },
  async seedDefaults({ em, tenantId }) {
    await backfillOrganizationSlugs(em, tenantId);
  }
};
var setup_default = setup;
export {
  setup_default as default,
  setup
};
//# sourceMappingURL=setup.js.map
