import { defineComponent, ref, computed, provide, watch, resolveComponent, createElementBlock, openBlock, normalizeClass, unref, createVNode, withCtx, withDirectives, normalizeProps, guardReactiveProps, vShow } from 'vue';
import { Form } from 'ant-design-vue';
import './config/index.mjs';
import '../../../utils/index.mjs';
import './components/api-url.vue.mjs';
import './components/params-table.vue.mjs';
import './components/body-component.vue.mjs';
import { createNamespace } from '../../../utils/create-namespace.mjs';
import { requestList, apiComponentEmits, apiComponentProps } from './config/api-component.mjs';
import _sfc_main$1 from './components/api-url.vue2.mjs';
import _sfc_main$2 from './components/params-table.vue2.mjs';
import { paramsTableColumns, headersTableColumns } from './config/table.mjs';
import _sfc_main$3 from './components/body-component.vue2.mjs';

"use strict";
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{ name: "LlApiComponent" },
  __name: "api-component",
  props: apiComponentProps,
  emits: apiComponentEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace("api-component");
    const useForm = Form.useForm;
    const formItemContext = Form.useInjectFormItemContext();
    const form = ref();
    const activeKey = ref("params");
    const computedRules = computed(() => props.isValid ? props.rules : {});
    provide("disabled", props.disabled);
    const httpModel = ref(props.value);
    const computedRequestList = computed(() => {
      if (httpModel.value.method === "GET") {
        return requestList.filter((item) => item.key !== "body");
      }
      return requestList;
    });
    const { resetFields, validate, validateInfos } = useForm(
      httpModel,
      computedRules.value
      // {
      //   onValidate: (...args) => {
      //     console.log('onValidate', args)
      //   }
      // }
    );
    const handleChange = (params) => {
      httpModel.value.method = params.method;
      httpModel.value.url = params.url;
      formItemContext.onFieldChange();
      setValue();
    };
    const setValue = () => {
      emits("change", httpModel.value);
      emits("update:value", httpModel.value);
    };
    watch(
      () => props.value,
      (val) => {
        httpModel.value = val;
      },
      {
        deep: true
      }
    );
    watch(
      () => httpModel,
      () => {
        setValue();
      },
      {
        deep: true
      }
    );
    __expose({
      validate,
      resetFields
    });
    return (_ctx, _cache) => {
      const _component_ll_form_item = resolveComponent("ll-form-item");
      const _component_a_form = resolveComponent("a-form");
      return openBlock(), createElementBlock(
        "div",
        {
          class: normalizeClass(unref(bem).b())
        },
        [
          createVNode(
            _component_a_form,
            {
              ref_key: "form",
              ref: form
            },
            {
              default: withCtx(() => [
                createVNode(
                  _component_ll_form_item,
                  normalizeProps(guardReactiveProps(unref(validateInfos).url)),
                  {
                    default: withCtx(() => [
                      createVNode(_sfc_main$1, {
                        value: httpModel.value,
                        "onUpdate:value": _cache[0] || (_cache[0] = ($event) => httpModel.value = $event),
                        class: normalizeClass(unref(bem).m("header")),
                        onChange: handleChange
                      }, null, 8, ["value", "class"])
                    ]),
                    _: 1
                    /* STABLE */
                  },
                  16
                  /* FULL_PROPS */
                ),
                withDirectives(createVNode(_sfc_main$2, {
                  columns: unref(paramsTableColumns),
                  "data-source": httpModel.value.requestParams
                }, null, 8, ["columns", "data-source"]), [
                  [vShow, activeKey.value === "params"]
                ]),
                withDirectives(createVNode(_sfc_main$2, {
                  columns: unref(headersTableColumns),
                  "data-source": httpModel.value.headerParams
                }, null, 8, ["columns", "data-source"]), [
                  [vShow, activeKey.value === "headers"]
                ]),
                withDirectives(createVNode(_sfc_main$3, {
                  value: httpModel.value,
                  "onUpdate:value": _cache[1] || (_cache[1] = ($event) => httpModel.value = $event)
                }, null, 8, ["value"]), [
                  [vShow, activeKey.value === "body"]
                ])
              ]),
              _: 1
              /* STABLE */
            },
            512
            /* NEED_PATCH */
          )
        ],
        2
        /* CLASS */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=api-component.vue2.mjs.map
