import { geliosCapabilityCatalogV2 } from "./capability-catalog-v2.mjs";

const IDENTITY_PRODUCT_ID = "fleet.units.identity.current.v1";
const IMPLEMENTED_FAMILIES = new Set([
  "gelios.unit.access_metadata",
  "gelios.unit.asset_identifiers",
  "gelios.unit.asset_profile",
  "gelios.unit.custom_fields",
  "gelios.unit.device_identifiers",
  "gelios.unit.driver",
  "gelios.unit.equipment",
  "gelios.unit.identity",
]);

export const geliosCapabilityCatalogV3 = deepFreeze(updateFamilies({
  ...structuredClone(geliosCapabilityCatalogV2),
  version: "3.0.0",
  authority: {
    ...geliosCapabilityCatalogV2.authority,
    checkedAt: "2026-07-24T08:00:00Z",
  },
}));

function updateFamilies(value) {
  value.dataFamilies = value.dataFamilies.map((family) => {
    if (!IMPLEMENTED_FAMILIES.has(family.id)) return family;
    return {
      ...family,
      disposition: "profile",
      status: "implemented",
      dataProductIds: [
        ...new Set([...(family.dataProductIds || []), IDENTITY_PRODUCT_ID]),
      ],
    };
  });
  return value;
}

function deepFreeze(input) {
  if (!input || typeof input !== "object" || Object.isFrozen(input)) return input;
  Object.freeze(input);
  for (const child of Object.values(input)) deepFreeze(child);
  return input;
}
