import { AttachmentPartition } from "../data/entities.js";
import { resolveDefaultAttachmentOcrEnabled } from "./ocrConfig.js";
import { parseBooleanToken } from "@open-mercato/shared/lib/boolean";
import { E } from "../../../generated/entities.ids.generated.js";
const DEFAULT_ATTACHMENT_PARTITIONS = [
  {
    code: "productsMedia",
    title: "Products media",
    description: "Public media uploaded for catalog products.",
    isPublic: true
  },
  {
    code: "privateAttachments",
    title: "Private attachments",
    description: "Internal attachments scoped to tenants and organizations.",
    isPublic: false
  }
];
const PRODUCT_MEDIA_ENTITY_IDS = new Set(
  [E.catalog?.catalog_product].filter(Boolean)
);
const FALLBACK_PARTITION = "privateAttachments";
function resolveDefaultPartitionCode(entityId) {
  if (!entityId) return FALLBACK_PARTITION;
  if (PRODUCT_MEDIA_ENTITY_IDS.has(entityId)) return "productsMedia";
  return FALLBACK_PARTITION;
}
async function ensureDefaultPartitions(em) {
  const repo = em.getRepository(AttachmentPartition);
  const existing = await repo.findAll({ fields: ["code"] });
  const existingCodes = new Set(existing.map((entry) => entry.code));
  const pending = DEFAULT_ATTACHMENT_PARTITIONS.filter((seed) => !existingCodes.has(seed.code));
  if (!pending.length) return;
  for (const seed of pending) {
    const record = repo.create({
      code: seed.code,
      title: seed.title,
      description: seed.description ?? null,
      storageDriver: "local",
      isPublic: seed.isPublic ?? false,
      requiresOcr: resolveDefaultAttachmentOcrEnabled()
    });
    em.persist(record);
  }
  await em.flush();
}
function sanitizePartitionCode(input) {
  const trimmed = input.trim();
  const normalized = trimmed.replace(/[^a-zA-Z0-9_-]/g, "");
  return normalized;
}
function isPartitionSettingsLocked() {
  const demoModeParsed = parseBooleanToken(process.env.DEMO_MODE ?? "");
  const demoModeEnabled = demoModeParsed === false ? false : true;
  const onboardingEnabled = parseBooleanToken(process.env.SELF_SERVICE_ONBOARDING_ENABLED ?? "") === true;
  return demoModeEnabled || onboardingEnabled;
}
export {
  DEFAULT_ATTACHMENT_PARTITIONS,
  ensureDefaultPartitions,
  isPartitionSettingsLocked,
  resolveDefaultPartitionCode,
  sanitizePartitionCode
};
//# sourceMappingURL=partitions.js.map
