"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
require("../../../../../../../../../../node_modules/.pnpm/vue@3.3.4/node_modules/vue/dist/vue.runtime.esm-bundler.js");
const runtimeCore_esmBundler = require("../../../../../../../../../../node_modules/.pnpm/@vue_runtime-core@3.3.4/node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js");
const reactivity_esmBundler = require("../../../../../../../../../../node_modules/.pnpm/@vue_reactivity@3.3.4/node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js");
const shared_esmBundler = require("../../../../../../../../../../node_modules/.pnpm/@vue_shared@3.3.4/node_modules/@vue/shared/dist/shared.esm-bundler.js");
const _hoisted_1 = /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("thead", null, [
  /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("tr", null, [
    /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("th"),
    /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("th", null, "Key"),
    /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("th", null, "Value"),
    /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("th", null, "操作"),
    /* @__PURE__ */ runtimeCore_esmBundler.createElementVNode("th", null, "结果")
  ])
], -1);
const _hoisted_2 = { style: { "width": "80px" } };
const _sfc_main = /* @__PURE__ */ runtimeCore_esmBundler.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;
    reactivity_esmBundler.ref(false);
    const defaultItem = {
      key: "",
      value: "",
      error: false
    };
    const tableArray = reactivity_esmBundler.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();
    };
    runtimeCore_esmBundler.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 = runtimeCore_esmBundler.resolveComponent("n-input");
      const _component_n_button = runtimeCore_esmBundler.resolveComponent("n-button");
      const _component_n_table = runtimeCore_esmBundler.resolveComponent("n-table");
      return runtimeCore_esmBundler.openBlock(), runtimeCore_esmBundler.createBlock(_component_n_table, {
        class: "go-request-header-table-box",
        "single-line": false,
        size: "small"
      }, {
        default: runtimeCore_esmBundler.withCtx(() => [
          _hoisted_1,
          runtimeCore_esmBundler.createElementVNode("tbody", null, [
            (runtimeCore_esmBundler.openBlock(true), runtimeCore_esmBundler.createElementBlock(runtimeCore_esmBundler.Fragment, null, runtimeCore_esmBundler.renderList(tableArray.content, (item, index) => {
              return runtimeCore_esmBundler.openBlock(), runtimeCore_esmBundler.createElementBlock("tr", { key: index }, [
                runtimeCore_esmBundler.createElementVNode("td", null, shared_esmBundler.toDisplayString(index + 1), 1),
                runtimeCore_esmBundler.createElementVNode("td", null, [
                  runtimeCore_esmBundler.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"])
                ]),
                runtimeCore_esmBundler.createElementVNode("td", null, [
                  runtimeCore_esmBundler.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"])
                ]),
                runtimeCore_esmBundler.createElementVNode("td", null, [
                  runtimeCore_esmBundler.createElementVNode("div", _hoisted_2, [
                    runtimeCore_esmBundler.createVNode(_component_n_button, {
                      class: "go-ml-2",
                      type: "primary",
                      size: "small",
                      ghost: "",
                      disabled: __props.editDisabled,
                      onClick: ($event) => add(index)
                    }, {
                      default: runtimeCore_esmBundler.withCtx(() => [
                        runtimeCore_esmBundler.createTextVNode("+")
                      ]),
                      _: 2
                    }, 1032, ["disabled", "onClick"]),
                    runtimeCore_esmBundler.createVNode(_component_n_button, {
                      class: "go-ml-2",
                      type: "warning",
                      size: "small",
                      ghost: "",
                      disabled: index === 0 && __props.editDisabled,
                      onClick: ($event) => remove(index)
                    }, {
                      default: runtimeCore_esmBundler.withCtx(() => [
                        runtimeCore_esmBundler.createTextVNode(" - ")
                      ]),
                      _: 2
                    }, 1032, ["disabled", "onClick"])
                  ])
                ]),
                runtimeCore_esmBundler.createElementVNode("td", null, [
                  item.error ? (runtimeCore_esmBundler.openBlock(), runtimeCore_esmBundler.createBlock(_component_n_button, {
                    key: 0,
                    class: "go-ml-2",
                    text: "",
                    type: "error"
                  }, {
                    default: runtimeCore_esmBundler.withCtx(() => [
                      runtimeCore_esmBundler.createTextVNode(" 格式错误 ")
                    ]),
                    _: 1
                  })) : (runtimeCore_esmBundler.openBlock(), runtimeCore_esmBundler.createBlock(_component_n_button, {
                    key: 1,
                    class: "go-ml-2",
                    text: "",
                    type: "primary"
                  }, {
                    default: runtimeCore_esmBundler.withCtx(() => [
                      runtimeCore_esmBundler.createTextVNode(" 格式通过 ")
                    ]),
                    _: 1
                  }))
                ])
              ]);
            }), 128))
          ])
        ]),
        _: 1
      });
    };
  }
});
exports.default = _sfc_main;
