function isPlainObject(value) {
  return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
}
function toComponentName(hint, fallbackIndex) {
  const cleaned = hint.split(/[^a-zA-Z0-9]+/).filter(Boolean).map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)).join("");
  return cleaned.length ? cleaned : `Schema${fallbackIndex}`;
}
const SCHEMA_KEYS = /* @__PURE__ */ new Set([
  "type",
  "properties",
  "items",
  "anyOf",
  "oneOf",
  "allOf",
  "$ref",
  "enum",
  "format",
  "not",
  "additionalProperties",
  "required",
  "pattern",
  "minimum",
  "maximum"
]);
function looksLikeSchema(value) {
  if (typeof value.$ref === "string") return true;
  for (const key of Object.keys(value)) {
    if (SCHEMA_KEYS.has(key)) return true;
  }
  return false;
}
function sanitizeOpenApiDocument(doc) {
  const target = doc;
  const schemaNameMap = /* @__PURE__ */ new Map();
  const schemas = target.components?.schemas ?? {};
  if (!target.components) target.components = {};
  target.components.schemas = schemas;
  let counter = Object.keys(schemas).length;
  const cloneContainer = (source, hint, mode) => {
    const clone = {};
    for (const [key, value] of Object.entries(source)) {
      const forceSchema = mode === "properties" || mode === "patternProperties";
      clone[key] = cloneSchemaValue(value, `${hint}_${key}`, key, forceSchema);
    }
    return clone;
  };
  const serializeSchema = (schema, hint) => {
    if (schemaNameMap.has(schema)) {
      const name2 = schemaNameMap.get(schema);
      return { $ref: `#/components/schemas/${name2}` };
    }
    counter += 1;
    const tentativeName = toComponentName(hint, counter);
    let name = tentativeName;
    let suffix = 1;
    while (schemas[name]) {
      name = `${tentativeName}${suffix++}`;
    }
    schemaNameMap.set(schema, name);
    schemas[name] = cloneContainer(schema, hint);
    return { $ref: `#/components/schemas/${name}` };
  };
  const cloneSchemaValue = (value, hint, parentKey, forceSchema = false) => {
    if (Array.isArray(value)) {
      return value.map((entry, idx) => cloneSchemaValue(entry, `${hint}_${idx}`, parentKey, forceSchema));
    }
    if (isPlainObject(value)) {
      if (typeof value.$ref === "string") return value;
      if (parentKey === "properties" || parentKey === "patternProperties") {
        return cloneContainer(value, hint, parentKey);
      }
      if (parentKey === "additionalProperties" && Object.keys(value).length) {
        return serializeSchema(value, hint);
      }
      if (forceSchema || looksLikeSchema(value)) {
        return serializeSchema(value, hint);
      }
      return cloneContainer(value, hint);
    }
    if (typeof value === "bigint") {
      return value.toString();
    }
    return value;
  };
  const traverse = (value, hint) => {
    if (Array.isArray(value)) {
      return value.map((entry, idx) => traverse(entry, `${hint}_${idx}`));
    }
    if (!isPlainObject(value)) {
      if (typeof value === "bigint") {
        return value.toString();
      }
      return value;
    }
    const result = { ...value };
    for (const [key, child] of Object.entries(result)) {
      if (key === "schema" && child && typeof child === "object") {
        result[key] = cloneSchemaValue(child, `${hint}_${key}`, key);
      } else {
        result[key] = traverse(child, `${hint}_${key}`);
      }
    }
    return result;
  };
  return traverse(target, "doc");
}
export {
  sanitizeOpenApiDocument
};
//# sourceMappingURL=sanitize.js.map
