import "../../chunks/chunk.QU3DSPNU.js";
import { CATEGORIES, FRAMEWORKS, GUIDE_CATEGORIES } from "./constants.js";
import { ValidationError } from "./errors.js";
function validateEnum(value, allowed, fieldName) {
  if (typeof value !== "string") {
    throw new ValidationError(`${fieldName} must be a string`, fieldName, {
      received: typeof value,
      allowed
    });
  }
  if (!allowed.includes(value)) {
    throw new ValidationError(
      `Invalid ${fieldName}: ${value}. Must be one of: ${allowed.join(", ")}`,
      fieldName,
      { received: value, allowed }
    );
  }
  return value;
}
function validateFramework(framework) {
  return validateEnum(framework, FRAMEWORKS, "framework");
}
function validateCategory(category) {
  return validateEnum(category, CATEGORIES, "category");
}
function validateGuideCategory(category) {
  return validateEnum(category, GUIDE_CATEGORIES, "guideCategory");
}
function validateString(value, fieldName, required = true, minLength, maxLength) {
  if (value === void 0 || value === null) {
    if (required) {
      throw new ValidationError(`${fieldName} is required`, fieldName);
    }
    return "";
  }
  if (typeof value !== "string") {
    throw new ValidationError(`${fieldName} must be a string`, fieldName, {
      received: typeof value
    });
  }
  const trimmed = value.trim();
  if (required && trimmed.length === 0) {
    throw new ValidationError(`${fieldName} cannot be empty`, fieldName);
  }
  if (minLength !== void 0 && trimmed.length < minLength) {
    throw new ValidationError(
      `${fieldName} must be at least ${minLength} characters`,
      fieldName,
      { received: trimmed.length, minLength }
    );
  }
  if (maxLength !== void 0 && trimmed.length > maxLength) {
    throw new ValidationError(
      `${fieldName} must be at most ${maxLength} characters`,
      fieldName,
      { received: trimmed.length, maxLength }
    );
  }
  return trimmed;
}
function validateBoolean(value, fieldName, defaultValue = false) {
  if (value === void 0 || value === null) {
    return defaultValue;
  }
  if (typeof value !== "boolean") {
    throw new ValidationError(`${fieldName} must be a boolean`, fieldName, {
      received: typeof value
    });
  }
  return value;
}
function validateNumber(value, fieldName, defaultValue, min, max) {
  if (value === void 0 || value === null) {
    if (defaultValue !== void 0) {
      return defaultValue;
    }
    throw new ValidationError(`${fieldName} is required`, fieldName);
  }
  if (typeof value !== "number" || isNaN(value)) {
    throw new ValidationError(`${fieldName} must be a number`, fieldName, {
      received: typeof value
    });
  }
  if (min !== void 0 && value < min) {
    throw new ValidationError(
      `${fieldName} must be at least ${min}`,
      fieldName,
      { received: value, min }
    );
  }
  if (max !== void 0 && value > max) {
    throw new ValidationError(
      `${fieldName} must be at most ${max}`,
      fieldName,
      { received: value, max }
    );
  }
  return value;
}
function sanitizeComponentName(name) {
  const sanitized = name.replace(/[^a-z0-9-]/gi, "");
  if (sanitized.length === 0) {
    throw new ValidationError(
      "Component name contains only invalid characters",
      "componentName",
      { original: name }
    );
  }
  if (sanitized !== name) {
    console.warn("[MCP Warning] Component name was sanitized:", {
      original: name,
      sanitized
    });
  }
  return sanitized;
}
function validateSearchComponentsInput(input) {
  if (!input || typeof input !== "object") {
    throw new ValidationError("Invalid input: must be an object", "input");
  }
  const obj = input;
  return {
    query: validateString(obj.query, "query", true, 1),
    category: obj.category ? validateCategory(obj.category) : "all",
    splitTerms: validateBoolean(obj.splitTerms, "splitTerms", true),
    matchAll: validateBoolean(obj.matchAll, "matchAll", false),
    useRegex: validateBoolean(obj.useRegex, "useRegex", false),
    maxResults: validateNumber(obj.maxResults, "maxResults", 20, 1, 100)
  };
}
function validateGetComponentDocsInput(input) {
  if (!input || typeof input !== "object") {
    throw new ValidationError("Invalid input: must be an object", "input");
  }
  const obj = input;
  const componentName = validateString(obj.componentName, "componentName");
  return {
    componentName: sanitizeComponentName(componentName),
    framework: validateFramework(obj.framework),
    includeGuidelines: validateBoolean(
      obj.includeGuidelines,
      "includeGuidelines",
      true
    ),
    includeInstructions: validateBoolean(
      obj.includeInstructions,
      "includeInstructions",
      true
    )
  };
}
function validateListGuidesInput(input) {
  if (!input || typeof input !== "object") {
    throw new ValidationError("Invalid input: must be an object", "input");
  }
  const obj = input;
  const result = {};
  if (obj.category !== void 0) {
    result.category = validateGuideCategory(obj.category);
  }
  if (obj.framework !== void 0 && obj.framework !== "all") {
    result.framework = validateEnum(
      obj.framework,
      ["angular", "react", "all"],
      "framework"
    );
  }
  return result;
}
function validateGetGuideInput(input) {
  if (!input || typeof input !== "object") {
    throw new ValidationError("Invalid input: must be an object", "input");
  }
  const obj = input;
  const name = validateString(obj.name, "name");
  const sanitized = name.replace(/[^a-z0-9-]/gi, "");
  if (sanitized.length === 0) {
    throw new ValidationError(
      "Guide name contains only invalid characters",
      "name",
      { original: name }
    );
  }
  return {
    name: sanitized
  };
}
export {
  sanitizeComponentName,
  validateBoolean,
  validateCategory,
  validateFramework,
  validateGetComponentDocsInput,
  validateGetGuideInput,
  validateGuideCategory,
  validateListGuidesInput,
  validateNumber,
  validateSearchComponentsInput,
  validateString
};
