import { CustomFieldDef } from "@open-mercato/core/modules/entities/data/entities";
const CUSTOMER_ENTITY_ID = "customers:customer_entity";
const PERSON_ENTITY_ID = "customers:customer_person_profile";
const COMPANY_ENTITY_ID = "customers:customer_company_profile";
function normalizeCustomFieldConfig(raw) {
  if (!raw) return {};
  if (typeof raw === "string") {
    try {
      const parsed = JSON.parse(raw);
      return parsed && typeof parsed === "object" ? parsed : {};
    } catch {
      return {};
    }
  }
  if (typeof raw === "object") {
    return { ...raw };
  }
  return {};
}
function scoreDefinition(kind, cfg, entityIndex) {
  const listVisibleScore = cfg.listVisible === false ? 0 : 1;
  const formEditableScore = cfg.formEditable === false ? 0 : 1;
  const filterableScore = cfg.filterable ? 1 : 0;
  const kindScore = (() => {
    switch (kind) {
      case "dictionary":
        return 8;
      case "relation":
        return 6;
      case "select":
        return 4;
      case "multiline":
        return 3;
      case "boolean":
      case "integer":
      case "float":
        return 2;
      default:
        return 1;
    }
  })();
  const optionsBonus = Array.isArray(cfg.options) && cfg.options.length ? 2 : 0;
  const dictionaryBonus = typeof cfg.dictionaryId === "string" && cfg.dictionaryId.trim().length ? 5 : 0;
  const base = listVisibleScore * 16 + formEditableScore * 8 + filterableScore * 4 + kindScore + optionsBonus + dictionaryBonus;
  const penalty = typeof cfg.priority === "number" ? cfg.priority : 0;
  return { base, penalty, entityIndex };
}
async function resolveProfileCustomFieldRouting(em, profileEntityId, tenantId, organizationId) {
  const entityIds = [CUSTOMER_ENTITY_ID, profileEntityId];
  const scopeClauses = [];
  if (tenantId) scopeClauses.push({ $or: [{ tenantId }, { tenantId: null }] });
  else scopeClauses.push({ tenantId: null });
  if (organizationId) scopeClauses.push({ $or: [{ organizationId }, { organizationId: null }] });
  const where = {
    entityId: { $in: entityIds },
    deletedAt: null,
    isActive: true
  };
  if (scopeClauses.length) where.$and = scopeClauses;
  const defs = await em.find(CustomFieldDef, where);
  const order = /* @__PURE__ */ new Map();
  entityIds.forEach((id, index) => order.set(id, index));
  const bestByKey = /* @__PURE__ */ new Map();
  for (const def of defs) {
    const cfg = normalizeCustomFieldConfig(def.configJson);
    const metrics = scoreDefinition(
      def.kind,
      cfg,
      order.get(def.entityId) ?? Number.MAX_SAFE_INTEGER
    );
    const existing = bestByKey.get(def.key);
    const better = !existing || metrics.base > existing.metrics.base || metrics.base === existing.metrics.base && (metrics.penalty < existing.metrics.penalty || metrics.penalty === existing.metrics.penalty && metrics.entityIndex < existing.metrics.entityIndex);
    if (better) {
      bestByKey.set(def.key, { entityId: def.entityId, metrics });
    }
  }
  const routing = /* @__PURE__ */ new Map();
  for (const [key, entry] of bestByKey.entries()) {
    routing.set(key, entry.entityId);
  }
  return routing;
}
function mergeProfileCustomFieldValues(routing, entityValues, profileValues) {
  const merged = { ...entityValues };
  for (const [key, value] of Object.entries(profileValues)) {
    const normalizedKey = key.startsWith("cf_") ? key.slice(3) : key;
    const target = routing.get(normalizedKey);
    if (target === CUSTOMER_ENTITY_ID) {
      if (!Object.prototype.hasOwnProperty.call(merged, key)) {
        merged[key] = value;
      }
      continue;
    }
    merged[key] = value;
  }
  return merged;
}
function mergePersonCustomFieldValues(routing, entityValues, profileValues) {
  return mergeProfileCustomFieldValues(routing, entityValues, profileValues);
}
function mergeCompanyCustomFieldValues(routing, entityValues, profileValues) {
  return mergeProfileCustomFieldValues(routing, entityValues, profileValues);
}
function resolvePersonCustomFieldRouting(em, tenantId, organizationId) {
  return resolveProfileCustomFieldRouting(em, PERSON_ENTITY_ID, tenantId, organizationId);
}
function resolveCompanyCustomFieldRouting(em, tenantId, organizationId) {
  return resolveProfileCustomFieldRouting(em, COMPANY_ENTITY_ID, tenantId, organizationId);
}
export {
  COMPANY_ENTITY_ID,
  CUSTOMER_ENTITY_ID,
  PERSON_ENTITY_ID,
  mergeCompanyCustomFieldValues,
  mergePersonCustomFieldValues,
  resolveCompanyCustomFieldRouting,
  resolvePersonCustomFieldRouting
};
//# sourceMappingURL=customFieldRouting.js.map
