import { resolve } from "@scalar/workspace-store/resolve";
import { isArraySchema } from "@scalar/workspace-store/schemas/v3.1/strict/type-guards";
const formatArrayType = (itemType) => {
  if (!itemType) {
    return "array";
  }
  const wrappedItemType = itemType.includes(" | ") ? `(${itemType})` : itemType;
  return `array ${wrappedItemType}[]`;
};
const processArrayType = (value, isUnionType = false) => {
  if (!value.items) {
    return "array";
  }
  const itemType = getSchemaType(resolve.schema(value.items));
  const baseType = formatArrayType(itemType);
  if (isUnionType) {
    return baseType;
  }
  return value.nullable ? `${baseType} | null` : baseType;
};
const getSchemaType = (valueOrRef) => {
  if (!valueOrRef) {
    return "";
  }
  const value = resolve.schema(valueOrRef);
  if (value.const !== void 0) {
    return "const";
  }
  if ("type" in value && Array.isArray(value.type)) {
    if (value.type.includes("array") && value.items) {
      const arrayType = processArrayType(value, true);
      const otherTypes = value.type.filter((t) => t !== "array");
      return otherTypes.length > 0 ? `${arrayType} | ${otherTypes.join(" | ")}` : arrayType;
    }
    return value.type.join(" | ");
  }
  if (isArraySchema(value)) {
    return processArrayType(value, false);
  }
  if ("type" in value && value.type && value.contentEncoding) {
    return `${value.type} • ${value.contentEncoding}`;
  }
  return "type" in value ? value.type : "";
};
export {
  getSchemaType
};
