import { defineComponent, computed, ref, createElementBlock, createCommentVNode, openBlock, createElementVNode, createVNode, renderSlot, unref } from "vue";
import { ScalarMarkdown } from "@scalar/components";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
import { isTypeObject } from "../../../components/Content/Schema/helpers/is-type-object.js";
import { sortPropertyNames, reduceNamesToObject } from "../../../components/Content/Schema/helpers/sort-property-names.js";
import _sfc_main$1 from "./ContentTypeSelect.vue.js";
import Schema from "../../../components/Content/Schema/Schema.vue.js";
const _hoisted_1 = {
  key: 0,
  "aria-label": "Request Body",
  class: "request-body",
  role: "group"
};
const _hoisted_2 = { class: "request-body-header" };
const _hoisted_3 = { class: "request-body-title" };
const _hoisted_4 = {
  key: 0,
  class: "request-body-required"
};
const _hoisted_5 = {
  key: 0,
  class: "request-body-description"
};
const _hoisted_6 = {
  key: 0,
  class: "request-body-schema"
};
const _hoisted_7 = {
  key: 1,
  class: "request-body-schema"
};
const MAX_VISIBLE_PROPERTIES = 12;
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "RequestBody",
  props: {
    breadcrumb: {},
    requestBody: {},
    eventBus: {},
    options: {}
  },
  setup(__props) {
    const availableContentTypes = computed(
      () => Object.keys(__props.requestBody?.content ?? {})
    );
    const selectedContentType = ref("application/json");
    if (__props.requestBody?.content) {
      if (availableContentTypes.value[0]) {
        selectedContentType.value = availableContentTypes.value[0];
      }
    }
    const schema = computed(
      () => getResolvedRef(__props.requestBody?.content?.[selectedContentType.value]?.schema)
    );
    const partitionedSchema = computed(() => {
      if (!schema.value || !isTypeObject(schema.value)) {
        return null;
      }
      const sortedNames = sortPropertyNames(
        schema.value,
        schema.value.discriminator,
        {
          hideReadOnly: true,
          orderSchemaPropertiesBy: __props.options.orderSchemaPropertiesBy,
          orderRequiredPropertiesFirst: __props.options.orderRequiredPropertiesFirst
        }
      );
      if (sortedNames.length <= MAX_VISIBLE_PROPERTIES) {
        return null;
      }
      const { properties, ...schemaMetadata } = schema.value;
      if (!properties) {
        return null;
      }
      return {
        visibleProperties: {
          ...schemaMetadata,
          properties: reduceNamesToObject(
            sortedNames.slice(0, MAX_VISIBLE_PROPERTIES),
            properties
          )
        },
        collapsedProperties: {
          ...schemaMetadata,
          properties: reduceNamesToObject(
            sortedNames.slice(MAX_VISIBLE_PROPERTIES),
            properties
          )
        }
      };
    });
    const shouldRenderRequestBody = computed(
      () => Object.keys(__props.requestBody?.content ?? {}).length > 0 || __props.requestBody?.description || __props.requestBody?.required
    );
    return (_ctx, _cache) => {
      return _ctx.requestBody && shouldRenderRequestBody.value ? (openBlock(), createElementBlock("div", _hoisted_1, [
        createElementVNode("div", _hoisted_2, [
          createElementVNode("div", _hoisted_3, [
            renderSlot(_ctx.$slots, "title", {}, void 0, true),
            _ctx.requestBody.required ? (openBlock(), createElementBlock("div", _hoisted_4, " required ")) : createCommentVNode("", true)
          ]),
          createVNode(_sfc_main$1, {
            modelValue: selectedContentType.value,
            "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedContentType.value = $event),
            content: _ctx.requestBody.content
          }, null, 8, ["modelValue", "content"]),
          _ctx.requestBody.description ? (openBlock(), createElementBlock("div", _hoisted_5, [
            createVNode(unref(ScalarMarkdown), {
              value: _ctx.requestBody.description
            }, null, 8, ["value"])
          ])) : createCommentVNode("", true)
        ]),
        partitionedSchema.value ? (openBlock(), createElementBlock("div", _hoisted_6, [
          createVNode(unref(Schema), {
            breadcrumb: _ctx.breadcrumb,
            compact: "",
            eventBus: _ctx.eventBus,
            name: "Request Body",
            noncollapsible: "",
            options: {
              hideReadOnly: true,
              orderRequiredPropertiesFirst: _ctx.options.orderRequiredPropertiesFirst,
              orderSchemaPropertiesBy: _ctx.options.orderSchemaPropertiesBy
            },
            schema: partitionedSchema.value.visibleProperties
          }, null, 8, ["breadcrumb", "eventBus", "options", "schema"]),
          createVNode(unref(Schema), {
            additionalProperties: "",
            breadcrumb: _ctx.breadcrumb,
            compact: "",
            eventBus: _ctx.eventBus,
            name: "Request Body",
            options: {
              hideReadOnly: true,
              orderRequiredPropertiesFirst: _ctx.options.orderRequiredPropertiesFirst,
              orderSchemaPropertiesBy: _ctx.options.orderSchemaPropertiesBy
            },
            schema: partitionedSchema.value.collapsedProperties
          }, null, 8, ["breadcrumb", "eventBus", "options", "schema"])
        ])) : schema.value ? (openBlock(), createElementBlock("div", _hoisted_7, [
          createVNode(unref(Schema), {
            breadcrumb: _ctx.breadcrumb,
            compact: "",
            eventBus: _ctx.eventBus,
            hideReadOnly: true,
            name: "Request Body",
            noncollapsible: "",
            options: {
              hideReadOnly: true,
              orderRequiredPropertiesFirst: _ctx.options.orderRequiredPropertiesFirst,
              orderSchemaPropertiesBy: _ctx.options.orderSchemaPropertiesBy
            },
            schema: schema.value
          }, null, 8, ["breadcrumb", "eventBus", "options", "schema"])
        ])) : createCommentVNode("", true)
      ])) : createCommentVNode("", true);
    };
  }
});
export {
  _sfc_main as default
};
