import { computed as c, ref as q, watch as O } from "vue";
const V = Symbol("discriminator-context"), w = Symbol("example-context");
function j(r) {
  var e;
  if ((e = r == null ? void 0 : r.discriminator) != null && e.mapping)
    return r.discriminator.mapping;
}
function E(r) {
  var e;
  return (e = r == null ? void 0 : r.discriminator) == null ? void 0 : e.propertyName;
}
function D(r) {
  return (r == null ? void 0 : r.discriminator) !== void 0;
}
function I(r, e) {
  var o;
  if (!r || !e || r.type !== "object" || !r.properties)
    return null;
  for (const [p, i] of Object.entries(e))
    if (typeof i == "object" && i !== null && "discriminator" in i && ((o = i.discriminator) != null && o.propertyName)) {
      const n = i.discriminator.propertyName;
      if (r.properties[n])
        return p;
    }
  return null;
}
function x(r) {
  if (r)
    return r.type === "array" && r.items && typeof r.items == "object" && !("$ref" in r.items) ? r.items : r;
}
function T(r, e, o) {
  var l;
  if (!((l = r.discriminator) != null && l.mapping) || !e)
    return;
  const p = r.discriminator.mapping[e];
  if (!p)
    return;
  const i = p.split("/").pop();
  if (!i || !o[i])
    return;
  const n = o[i];
  if (n.allOf) {
    const d = {
      type: n.type || "object",
      properties: {},
      required: [],
      ...r.title ? { title: r.title } : {},
      ...r.name ? { name: r.name } : {}
    }, y = {}, a = /* @__PURE__ */ new Set();
    r.required && r.required.forEach((u) => a.add(u));
    for (const u of n.allOf)
      if ("$ref" in u) {
        const s = u.$ref.split("/").pop();
        if (s && o[s]) {
          const t = o[s];
          t.properties && Object.assign(y, t.properties), t.required && t.required.forEach((f) => a.add(f));
        }
      } else "properties" in u && (Object.assign(y, u.properties), "required" in u && Array.isArray(u.required) && u.required.forEach((s) => a.add(s)));
    return d.properties = y, d.required = Array.from(a), d;
  }
  return {
    type: n.type || r.type,
    properties: {
      ...r.properties || {},
      ...n.properties || {}
    },
    required: [...r.required || [], ...n.required || []],
    // Preserve discriminator
    discriminator: r.discriminator,
    // Preserve title and name from base schema
    ...r.title ? { title: r.title } : {},
    ...r.name ? { name: r.name } : {}
  };
}
function X({ schema: r, schemas: e, onSchemaChange: o }) {
  const p = c(() => x(r)), i = c(() => j(p.value)), n = c(() => i.value && Object.keys(i.value)[0] || ""), l = q(n.value);
  O(
    n,
    (t) => {
      t && !l.value && (l.value = t);
    },
    { immediate: !0 }
  );
  const d = c(() => E(p.value)), y = c(() => D(p.value)), a = c(() => {
    if (!p.value || !e || !l.value)
      return r;
    const t = T(
      p.value,
      l.value,
      e
    );
    if (!t)
      return r;
    const f = (r == null ? void 0 : r.type) === "array" ? {
      type: "array",
      items: t,
      ...r.title ? { title: r.title } : {},
      ...r.name ? { name: r.name } : {}
    } : t;
    return o == null || o(f), f;
  }), u = (t) => {
    if ("default" in t)
      return t.default;
    if ("example" in t)
      return t.example;
    switch (t.type) {
      case "string":
        return "";
      case "integer":
      case "number":
        return t.nullable ? null : 0;
      case "boolean":
        return !1;
      case "array":
        return [];
      case "object":
        return {};
      default:
        return null;
    }
  };
  return {
    selectedType: l,
    discriminatorMapping: i,
    defaultType: n,
    discriminatorPropertyName: d,
    hasDiscriminator: y,
    mergedSchema: a,
    generateExampleValue: (t = !1) => {
      if (!a.value)
        return t ? [] : {};
      const f = {}, v = a.value.type === "array" ? a.value.items : a.value;
      return "properties" in v && v.properties && Object.entries(v.properties).forEach(([g, N]) => {
        const m = N;
        m !== null && (f[g] = u(m));
      }), d.value && l.value && (f[d.value] = l.value), JSON.parse(JSON.stringify(t ? [f] : f));
    }
  };
}
export {
  V as DISCRIMINATOR_CONTEXT,
  w as EXAMPLE_CONTEXT,
  j as getDiscriminatorMapping,
  E as getDiscriminatorPropertyName,
  I as getDiscriminatorSchemaName,
  D as hasDiscriminator,
  T as mergeDiscriminatorSchemas,
  X as useDiscriminator
};
