import { ATOMIC_COMPONENTS_REGISTRY } from "./atomic-components-registry.js";
function validatePattern(pattern) {
  const errors = [];
  const warnings = [];
  if (!pattern.id || pattern.id.trim() === "") {
    errors.push({
      field: "id",
      message: "Pattern ID is required",
      severity: "error"
    });
  }
  if (!pattern.name || pattern.name.trim() === "") {
    errors.push({
      field: "name",
      message: "Pattern name is required",
      severity: "error"
    });
  }
  if (!pattern.version || !isValidVersion(pattern.version)) {
    errors.push({
      field: "version",
      message: "Pattern version must be in semver format (e.g., 1.0.0)",
      severity: "error"
    });
  }
  if (!pattern.description || pattern.description.trim() === "") {
    warnings.push({
      field: "description",
      message: "Pattern description is recommended for clarity"
    });
  }
  const validCategories = ["data-entry", "data-display", "dashboard", "navigation", "workflow"];
  if (!validCategories.includes(pattern.category)) {
    errors.push({
      field: "category",
      message: `Category must be one of: ${validCategories.join(", ")}`,
      severity: "error"
    });
  }
  if (!pattern.requiredAtomicComponents || pattern.requiredAtomicComponents.length === 0) {
    errors.push({
      field: "requiredAtomicComponents",
      message: "At least one required atomic component must be specified",
      severity: "error"
    });
  } else {
    for (const component of pattern.requiredAtomicComponents) {
      if (!ATOMIC_COMPONENTS_REGISTRY[component]) {
        errors.push({
          field: "requiredAtomicComponents",
          message: `Required component '${component}' not found in ATOMIC_COMPONENTS_REGISTRY`,
          severity: "error"
        });
      }
    }
  }
  if (pattern.optionalAtomicComponents) {
    for (const component of pattern.optionalAtomicComponents) {
      if (!ATOMIC_COMPONENTS_REGISTRY[component]) {
        warnings.push({
          field: "optionalAtomicComponents",
          message: `Optional component '${component}' not found in ATOMIC_COMPONENTS_REGISTRY`
        });
      }
    }
  }
  if (!pattern.layoutStrategy) {
    errors.push({
      field: "layoutStrategy",
      message: "Layout strategy is required",
      severity: "error"
    });
  } else {
    const validLayoutTypes = ["single-column", "two-column", "grid", "flex", "masonry", "custom"];
    if (!validLayoutTypes.includes(pattern.layoutStrategy.type)) {
      errors.push({
        field: "layoutStrategy.type",
        message: `Layout type must be one of: ${validLayoutTypes.join(", ")}`,
        severity: "error"
      });
    }
  }
  if (!pattern.dataBindings) {
    errors.push({
      field: "dataBindings",
      message: "Data bindings are required",
      severity: "error"
    });
  } else {
    const validSources = ["model", "controller", "service", "store", "computed"];
    if (!validSources.includes(pattern.dataBindings.source)) {
      errors.push({
        field: "dataBindings.source",
        message: `Data source must be one of: ${validSources.join(", ")}`,
        severity: "error"
      });
    }
    if (!pattern.dataBindings.operations || pattern.dataBindings.operations.length === 0) {
      errors.push({
        field: "dataBindings.operations",
        message: "At least one data operation must be specified",
        severity: "error"
      });
    } else {
      for (const op of pattern.dataBindings.operations) {
        const validOps = ["create", "update", "retrieve", "retrieve_many", "validate", "evolve", "delete"];
        if (!validOps.includes(op.operation)) {
          errors.push({
            field: "dataBindings.operations",
            message: `Operation '${op.operation}' must be one of: ${validOps.join(", ")}`,
            severity: "error"
          });
        }
      }
    }
  }
  if (!pattern.supportedOperations || pattern.supportedOperations.length === 0) {
    errors.push({
      field: "supportedOperations",
      message: "At least one supported operation must be specified",
      severity: "error"
    });
  }
  if (pattern.eventHandlers) {
    for (const handler of pattern.eventHandlers) {
      if (!handler.event) {
        errors.push({
          field: "eventHandlers",
          message: "Event handler must specify an event name",
          severity: "error"
        });
      }
      if (!handler.operation && !handler.handler) {
        errors.push({
          field: "eventHandlers",
          message: `Event handler for '${handler.event}' must specify either operation or handler`,
          severity: "error"
        });
      }
    }
  }
  if (pattern.dataBindings?.relationships) {
    for (const rel of pattern.dataBindings.relationships) {
      const validRelTypes = ["belongsTo", "hasMany", "hasOne", "manyToMany"];
      if (!validRelTypes.includes(rel.type)) {
        errors.push({
          field: "dataBindings.relationships",
          message: `Relationship type '${rel.type}' must be one of: ${validRelTypes.join(", ")}`,
          severity: "error"
        });
      }
      if (!rel.display?.labelField) {
        warnings.push({
          field: "dataBindings.relationships",
          message: `Relationship '${rel.name}' should specify a labelField for display`
        });
      }
    }
  }
  if (pattern.extends) {
    warnings.push({
      field: "extends",
      message: `Pattern extends '${pattern.extends}' - ensure parent pattern exists`
    });
  }
  if (pattern.customization) {
    if (pattern.customization.requiredOverrides && pattern.customization.requiredOverrides.length > 0) {
      const validFields = ["layoutStrategy", "sections", "operations", "relationships"];
      for (const override of pattern.customization.requiredOverrides) {
        if (!validFields.includes(override)) {
          warnings.push({
            field: "customization.requiredOverrides",
            message: `Override field '${override}' may not be a valid customization point`
          });
        }
      }
    }
  }
  return {
    valid: errors.length === 0,
    errors,
    warnings
  };
}
function isValidVersion(version) {
  const semverRegex = /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/;
  return semverRegex.test(version);
}
function validatePatternRegistry(registry) {
  const results = {};
  for (const [id, pattern] of Object.entries(registry)) {
    results[id] = validatePattern(pattern);
  }
  return results;
}
function getValidationSummary(results) {
  const total = Object.keys(results).length;
  const valid = Object.values(results).filter((r) => r.valid).length;
  const invalid = total - valid;
  const warnings = Object.values(results).reduce(
    (sum, r) => sum + r.warnings.length,
    0
  );
  return { total, valid, invalid, warnings };
}
export {
  getValidationSummary,
  validatePattern,
  validatePatternRegistry
};
