import "../../../../../../../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.mjs";
import { defineComponent, watch, resolveComponent, openBlock, createBlock, withCtx, createElementVNode as createBaseVNode, createElementBlock, Fragment, renderList, createVNode, createTextVNode } from "../../../../../../../../../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.mjs";
import { ref, reactive } from "../../../../../../../../../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.mjs";
import { toDisplayString } from "../../../../../../../../../../node_modules/.pnpm/@vue_shared@3.3.4/node_modules/@vue/shared/dist/shared.esm-bundler.mjs";
const _hoisted_1 = /* @__PURE__ */ createBaseVNode("thead", null, [
  /* @__PURE__ */ createBaseVNode("tr", null, [
    /* @__PURE__ */ createBaseVNode("th"),
    /* @__PURE__ */ createBaseVNode("th", null, "Key"),
    /* @__PURE__ */ createBaseVNode("th", null, "Value"),
    /* @__PURE__ */ createBaseVNode("th", null, "操作"),
    /* @__PURE__ */ createBaseVNode("th", null, "结果")
  ])
], -1);
const _hoisted_2 = { style: { "width": "80px" } };
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    target: {
      type: Object,
      required: true,
      default: () => {
      }
    },
    editDisabled: {
      type: Boolean,
      required: false,
      default: false
    }
  },
  emits: ["update"],
  setup(__props, { emit: emits }) {
    const props = __props;
    ref(false);
    const defaultItem = {
      key: "",
      value: "",
      error: false
    };
    const tableArray = reactive({ content: [] });
    const blur = () => {
      let successNum = 0;
      tableArray.content.forEach((item) => {
        if (item.key !== "" && item.value == "" || item.key === "" && item.value !== "") {
          item.error = true;
        } else {
          successNum++;
          item.error = false;
        }
      });
      if (successNum == tableArray.content.length) {
        const updateObj = {};
        tableArray.content.forEach((e) => {
          if (e.key)
            updateObj[e.key] = e.value;
        });
        emits("update", updateObj);
      }
    };
    const add = (index) => {
      tableArray.content.splice(index + 1, 0, {
        key: "",
        value: "",
        error: false
      });
    };
    const remove = (index) => {
      if (tableArray.content.length !== 1) {
        tableArray.content.splice(index, 1);
      }
      blur();
    };
    watch(
      () => props.target,
      (target) => {
        tableArray.content = [];
        for (const k in target) {
          tableArray.content.push({
            key: k,
            value: target[k],
            error: false
          });
        }
        if (!tableArray.content.length)
          tableArray.content = [JSON.parse(JSON.stringify(defaultItem))];
      },
      {
        immediate: true,
        deep: true
      }
    );
    return (_ctx, _cache) => {
      const _component_n_input = resolveComponent("n-input");
      const _component_n_button = resolveComponent("n-button");
      const _component_n_table = resolveComponent("n-table");
      return openBlock(), createBlock(_component_n_table, {
        class: "go-request-header-table-box",
        "single-line": false,
        size: "small"
      }, {
        default: withCtx(() => [
          _hoisted_1,
          createBaseVNode("tbody", null, [
            (openBlock(true), createElementBlock(Fragment, null, renderList(tableArray.content, (item, index) => {
              return openBlock(), createElementBlock("tr", { key: index }, [
                createBaseVNode("td", null, toDisplayString(index + 1), 1),
                createBaseVNode("td", null, [
                  createVNode(_component_n_input, {
                    value: item.key,
                    "onUpdate:value": ($event) => item.key = $event,
                    disabled: __props.editDisabled,
                    type: "text",
                    size: "small",
                    onBlur: blur
                  }, null, 8, ["value", "onUpdate:value", "disabled"])
                ]),
                createBaseVNode("td", null, [
                  createVNode(_component_n_input, {
                    value: item.value,
                    "onUpdate:value": ($event) => item.value = $event,
                    disabled: __props.editDisabled,
                    type: "text",
                    size: "small",
                    onBlur: blur
                  }, null, 8, ["value", "onUpdate:value", "disabled"])
                ]),
                createBaseVNode("td", null, [
                  createBaseVNode("div", _hoisted_2, [
                    createVNode(_component_n_button, {
                      class: "go-ml-2",
                      type: "primary",
                      size: "small",
                      ghost: "",
                      disabled: __props.editDisabled,
                      onClick: ($event) => add(index)
                    }, {
                      default: withCtx(() => [
                        createTextVNode("+")
                      ]),
                      _: 2
                    }, 1032, ["disabled", "onClick"]),
                    createVNode(_component_n_button, {
                      class: "go-ml-2",
                      type: "warning",
                      size: "small",
                      ghost: "",
                      disabled: index === 0 && __props.editDisabled,
                      onClick: ($event) => remove(index)
                    }, {
                      default: withCtx(() => [
                        createTextVNode(" - ")
                      ]),
                      _: 2
                    }, 1032, ["disabled", "onClick"])
                  ])
                ]),
                createBaseVNode("td", null, [
                  item.error ? (openBlock(), createBlock(_component_n_button, {
                    key: 0,
                    class: "go-ml-2",
                    text: "",
                    type: "error"
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" 格式错误 ")
                    ]),
                    _: 1
                  })) : (openBlock(), createBlock(_component_n_button, {
                    key: 1,
                    class: "go-ml-2",
                    text: "",
                    type: "primary"
                  }, {
                    default: withCtx(() => [
                      createTextVNode(" 格式通过 ")
                    ]),
                    _: 1
                  }))
                ])
              ]);
            }), 128))
          ])
        ]),
        _: 1
      });
    };
  }
});
export {
  _sfc_main as default
};
