"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/config/template-validation.ts
var template_validation_exports = {};
__export(template_validation_exports, {
  formatValidationErrors: () => formatValidationErrors,
  validateTemplateBindings: () => validateTemplateBindings
});
module.exports = __toCommonJS(template_validation_exports);
function collectFieldIds(fields) {
  const ids = /* @__PURE__ */ new Set();
  const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
  function traverse(fieldList, prefix = "") {
    for (const field of fieldList) {
      const fullId = prefix ? `${prefix}.${field.id}` : field.id;
      ids.add(fullId);
      const anyField = field;
      const schema = isRecord(anyField.schema) ? anyField.schema : null;
      const schemaFields = schema && Array.isArray(schema.fields) ? schema.fields : null;
      if (schemaFields) {
        traverse(schemaFields, fullId);
      }
      const tabs = Array.isArray(anyField.tabs) ? anyField.tabs : null;
      if (tabs) {
        for (const tab of tabs) {
          if (!isRecord(tab)) continue;
          const tabFields = Array.isArray(tab.fields) ? tab.fields : null;
          if (tabFields) {
            traverse(tabFields, fullId);
          }
        }
      }
      const itemTypes = isRecord(anyField.itemTypes) ? anyField.itemTypes : null;
      if (itemTypes) {
        for (const itemTypeDef of Object.values(itemTypes)) {
          if (!isRecord(itemTypeDef)) continue;
          const itemFields = Array.isArray(itemTypeDef.fields) ? itemTypeDef.fields : null;
          if (itemFields) {
            traverse(itemFields, fullId);
          }
        }
      }
      const legacyFields = Array.isArray(anyField.fields) ? anyField.fields : null;
      if (legacyFields) {
        traverse(legacyFields, fullId);
      }
    }
  }
  traverse(fields);
  return ids;
}
function validateBindingPath(binding, contentTypeFieldIds, blockIdentifier, fieldKey) {
  const errors = [];
  switch (binding.source) {
    case "field":
      if (!contentTypeFieldIds.has(binding.path)) {
        errors.push({
          blockIdentifier,
          fieldKey,
          message: `Field path "${binding.path}" does not exist in content type fields`,
          type: "invalid_field_path"
        });
      }
      break;
    case "meta":
      break;
    case "transform":
      if (!contentTypeFieldIds.has(binding.path)) {
        errors.push({
          blockIdentifier,
          fieldKey,
          message: `Transform source path "${binding.path}" does not exist in content type fields`,
          type: "invalid_field_path"
        });
      }
      break;
    case "fallback":
      for (const source of binding.sources) {
        errors.push(...validateBindingPath(source, contentTypeFieldIds, blockIdentifier, fieldKey));
      }
      break;
    case "literal":
      break;
  }
  return errors;
}
function collectManifestFieldIds(manifest) {
  return collectFieldIds(manifest.fields ?? []);
}
function validateTemplateBindings(blocks, contentTypeFields, getBlockManifest) {
  const errors = [];
  const contentTypeFieldIds = collectFieldIds(contentTypeFields);
  for (const block of blocks) {
    if (block.scope === "template") {
      continue;
    }
    if (!block.bindings) {
      continue;
    }
    const manifest = getBlockManifest(block.kind);
    if (!manifest) {
      errors.push({
        blockIdentifier: block.identifier,
        message: `Unknown block kind "${block.kind}"`,
        type: "invalid_block_kind"
      });
      continue;
    }
    const manifestFieldIds = collectManifestFieldIds(manifest);
    for (const [fieldKey, binding] of Object.entries(block.bindings)) {
      if (!manifestFieldIds.has(fieldKey)) {
        errors.push({
          blockIdentifier: block.identifier,
          fieldKey,
          message: `Binding key "${fieldKey}" does not exist in block "${block.kind}" fields`,
          type: "invalid_binding_key"
        });
      }
      errors.push(...validateBindingPath(binding, contentTypeFieldIds, block.identifier, fieldKey));
    }
  }
  return errors;
}
function formatValidationErrors(errors) {
  return errors.map((error) => {
    const location = error.fieldKey ? `[${error.blockIdentifier}.${error.fieldKey}]` : `[${error.blockIdentifier}]`;
    return `${location} ${error.message}`;
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatValidationErrors,
  validateTemplateBindings
});
//# sourceMappingURL=config-template-validation.js.map