import { defineComponent, computed, ref, createElementBlock, openBlock, createBlock, unref, Fragment, createVNode, createElementVNode, withCtx, createCommentVNode, toDisplayString, normalizeClass } from "vue";
import { ScalarListbox } from "@scalar/components";
import { isDefined } from "@scalar/helpers/array/is-defined";
import { ScalarIconCaretDown } from "@scalar/icons";
import { resolve } from "@scalar/workspace-store/resolve";
import { getSchemaType } from "./helpers/get-schema-type.js";
import { mergeAllOfSchemas } from "./helpers/merge-all-of-schemas.js";
import { getModelNameFromSchema } from "./helpers/schema-name.js";
import Schema from "./Schema.vue.js";
const _hoisted_1 = { class: "property-rule" };
const _hoisted_2 = {
  class: "composition-selector bg-b-1.5 hover:bg-b-2 flex w-full cursor-pointer items-center gap-1 rounded-t-lg border px-2.5 py-2.5 pr-3 text-left",
  type: "button"
};
const _hoisted_3 = { class: "text-c-2" };
const _hoisted_4 = {
  key: 0,
  class: "text-red"
};
const _hoisted_5 = { class: "composition-panel" };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "SchemaComposition",
  props: {
    composition: {},
    discriminator: {},
    name: {},
    schema: {},
    level: {},
    compact: { type: Boolean, default: false },
    hideHeading: { type: Boolean, default: false },
    breadcrumb: {},
    eventBus: {},
    options: {}
  },
  setup(__props) {
    const props = __props;
    const composition = computed(
      () => [props.schema[props.composition]].flat().map((schema) => ({ value: resolve.schema(schema), original: schema })).filter((it) => isDefined(it.value))
    );
    const listboxOptions = computed(
      () => composition.value.map((schema, index) => {
        const resolved = resolve.schema(schema.original);
        const label = (getModelNameFromSchema(resolved) ?? getSchemaType(resolved)) || "Schema";
        return { id: String(index), label };
      })
    );
    const selectedOption = ref(
      listboxOptions.value[0]
    );
    const humanizeType = (type) => type.replace(/([A-Z])/g, " $1").replace(/^./, (str) => str.toUpperCase()).toLowerCase().replace(/^(\w)/, (c) => c.toUpperCase());
    const selectedComposition = computed(
      () => composition.value[Number(selectedOption.value?.id ?? "0")]?.value
    );
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", _hoisted_1, [
        props.composition === "allOf" ? (openBlock(), createBlock(Schema, {
          key: 0,
          breadcrumb: __props.breadcrumb,
          compact: __props.compact,
          discriminator: __props.discriminator,
          eventBus: __props.eventBus,
          hideHeading: __props.hideHeading,
          level: __props.level,
          name: __props.name,
          noncollapsible: true,
          options: __props.options,
          schema: unref(mergeAllOfSchemas)(__props.schema)
        }, null, 8, ["breadcrumb", "compact", "discriminator", "eventBus", "hideHeading", "level", "name", "options", "schema"])) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
          createVNode(unref(ScalarListbox), {
            modelValue: selectedOption.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedOption.value = $event),
            options: listboxOptions.value,
            resize: ""
          }, {
            default: withCtx(() => [
              createElementVNode("button", _hoisted_2, [
                createElementVNode("span", _hoisted_3, toDisplayString(humanizeType(props.composition)), 1),
                createElementVNode("span", {
                  class: normalizeClass(["composition-selector-label text-c-1", {
                    "line-through": selectedComposition.value?.deprecated
                  }])
                }, toDisplayString(selectedOption.value?.label || "Schema"), 3),
                selectedComposition.value?.deprecated ? (openBlock(), createElementBlock("div", _hoisted_4, " deprecated ")) : createCommentVNode("", true),
                createVNode(unref(ScalarIconCaretDown))
              ])
            ]),
            _: 1
          }, 8, ["modelValue", "options"]),
          createElementVNode("div", _hoisted_5, [
            createVNode(Schema, {
              breadcrumb: __props.breadcrumb,
              compact: __props.compact,
              discriminator: __props.discriminator,
              eventBus: __props.eventBus,
              hideHeading: __props.hideHeading,
              level: __props.level + 1,
              name: __props.name,
              noncollapsible: true,
              options: __props.options,
              schema: selectedComposition.value
            }, null, 8, ["breadcrumb", "compact", "discriminator", "eventBus", "hideHeading", "level", "name", "options", "schema"])
          ])
        ], 64))
      ]);
    };
  }
});
export {
  _sfc_main as default
};
