'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var lodashEs = require('lodash-es');
require('../../../../utils/index.js');
require('../config/index.js');
require('./json-xml-table.vue.js');
var createNamespace = require('../../../../utils/create-namespace.js');
var jsonXml = require('../config/json-xml.js');
var jsonXmlTable_vue_vue_type_script_setup_true_lang = require('./json-xml-table.vue2.js');

"use strict";
const _hoisted_1 = { class: "flex" };
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlJsonXml" },
  __name: "json-xml",
  props: jsonXml.jsonXmlProps,
  emits: jsonXml.jsonXmlEmits,
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const bem = createNamespace.createNamespace("json-xml");
    const disabled = vue.inject("disabled");
    const type = vue.ref(jsonXml.DEFAULT_TYPE);
    const innerValue = vue.ref(JSON.stringify(props.value));
    const dataSource = vue.ref([
      {
        key: "1",
        name: "\u6839\u8282\u70B9",
        type: "object",
        value: "",
        chineseName: "",
        description: "",
        mock: "",
        children: []
      }
    ]);
    const parseJson2Array = (json, parentKey = "1", parentType = "object") => {
      let num = 0;
      const result = [];
      for (const key in json) {
        num++;
        if (Object.prototype.hasOwnProperty.call(json, key)) {
          const element = json[key];
          if (typeof element === "object" || Array.isArray(element)) {
            const currentType = Array.isArray(element) ? "array" : "object";
            result.push({
              key: `${parentKey}-${num}`,
              name: parentType === "array" ? "ITEM" : key,
              type: currentType,
              value: parentType === "array" && currentType !== "object" ? element : "",
              chineseName: "",
              description: "",
              mock: "",
              children: parseJson2Array(element, `${parentKey}-${num}`, currentType)
            });
          } else {
            result.push({
              key: `${parentKey}-${num}`,
              name: parentType === "array" ? "ITEM" : key,
              type: typeof element,
              value: "",
              chineseName: "",
              description: "",
              mock: element
            });
          }
        }
      }
      return result;
    };
    const parseArray2Json = (data) => {
      const result = {};
      data.forEach((item) => {
        if (item.name) {
          if (item.type === "object" || item.type === "array") {
            if (item.children && item.children.length) {
              result[item.name] = parseArray2Json(item.children);
            }
          } else {
            result[item.name] = item.mock;
          }
        }
      });
      return result;
    };
    const handleTypeChange = (e) => {
      if (e === "json") {
        innerValue.value = JSON.stringify(parseArray2Json(dataSource.value));
      } else {
        try {
          const value = JSON.parse(innerValue.value);
          if (typeof value === "object") {
            dataSource.value = parseJson2Array(value);
          } else {
            dataSource.value = [
              {
                key: "1",
                name: "\u6839\u8282\u70B9",
                type: typeof value,
                value: "",
                chineseName: "",
                description: "",
                mock: value,
                children: []
              }
            ];
          }
        } catch (error) {
          dataSource.value = [
            {
              key: "1",
              name: "\u6839\u8282\u70B9",
              type: typeof innerValue.value,
              value: "",
              chineseName: "",
              description: "",
              mock: innerValue.value,
              children: []
            }
          ];
        }
      }
    };
    const handleChange = (arr) => {
      dataSource.value = arr;
      emits("update:value", parseArray2Json(arr));
    };
    vue.watch(
      () => props.value,
      (n) => {
        if (!lodashEs.isEmpty(n)) {
          if (typeof n === "object") {
            innerValue.value = JSON.stringify(n);
            dataSource.value = parseJson2Array(props.value);
          } else {
            innerValue.value = n;
            dataSource.value = [
              {
                key: "1",
                name: "\u6839\u8282\u70B9",
                type: typeof props.value,
                value: "",
                chineseName: "",
                description: "",
                mock: n,
                children: []
              }
            ];
          }
        } else {
          innerValue.value = "";
          dataSource.value = [
            {
              key: "1",
              name: "\u6839\u8282\u70B9",
              type: "object",
              value: "",
              chineseName: "",
              description: "",
              mock: "",
              children: []
            }
          ];
        }
      },
      {
        deep: true,
        immediate: true
      }
    );
    vue.watch(innerValue, (n) => {
      try {
        const value = JSON.parse(n);
        if (typeof value === "object") {
          emits("update:value", value);
        } else {
          emits("update:value", value);
        }
      } catch (error) {
        emits("update:value", n);
      }
    });
    return (_ctx, _cache) => {
      const _component_ll_segmented = vue.resolveComponent("ll-segmented");
      const _component_ll_button = vue.resolveComponent("ll-button");
      const _component_ll_icon = vue.resolveComponent("ll-icon");
      const _component_ll_dropdown = vue.resolveComponent("ll-dropdown");
      const _component_ll_code_editor = vue.resolveComponent("ll-code-editor");
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          class: vue.normalizeClass(vue.unref(bem).b("json-xml"))
        },
        [
          vue.createElementVNode(
            "div",
            {
              class: vue.normalizeClass(vue.unref(bem).e("header"))
            },
            [
              vue.createVNode(_component_ll_segmented, {
                value: type.value,
                "onUpdate:value": _cache[0] || (_cache[0] = ($event) => type.value = $event),
                size: "middle",
                options: vue.unref(jsonXml.jsonXmlOptions),
                onChange: handleTypeChange
              }, {
                label: vue.withCtx(({ payload }) => [
                  vue.createElementVNode(
                    "div",
                    null,
                    vue.toDisplayString(payload.title),
                    1
                    /* TEXT */
                  )
                ]),
                _: 1
                /* STABLE */
              }, 8, ["value", "options"]),
              vue.createElementVNode("div", _hoisted_1, [
                type.value === "json" ? (vue.openBlock(), vue.createElementBlock(
                  vue.Fragment,
                  { key: 0 },
                  [
                    vue.createVNode(_component_ll_button, {
                      type: "link",
                      disabled: vue.unref(disabled)
                    }, {
                      default: vue.withCtx(() => [
                        vue.createCommentVNode(" <ll-icon/> "),
                        _cache[2] || (_cache[2] = vue.createTextVNode(
                          " \u9884\u89C8 ",
                          -1
                          /* CACHED */
                        ))
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled"]),
                    vue.createVNode(_component_ll_button, {
                      type: "link",
                      disabled: vue.unref(disabled)
                    }, {
                      default: vue.withCtx(() => [
                        vue.createCommentVNode(" <ll-icon/> "),
                        _cache[3] || (_cache[3] = vue.createTextVNode(
                          " \u751F\u6210\u4EE3\u7801 ",
                          -1
                          /* CACHED */
                        ))
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled"]),
                    vue.createVNode(_component_ll_button, {
                      type: "link",
                      disabled: vue.unref(disabled)
                    }, {
                      default: vue.withCtx(() => [
                        vue.createCommentVNode(" <ll-icon/> "),
                        _cache[4] || (_cache[4] = vue.createTextVNode(
                          " JSON Schema ",
                          -1
                          /* CACHED */
                        ))
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled"])
                  ],
                  64
                  /* STABLE_FRAGMENT */
                )) : (vue.openBlock(), vue.createElementBlock(
                  vue.Fragment,
                  { key: 1 },
                  [
                    vue.createVNode(_component_ll_button, {
                      type: "link",
                      disabled: vue.unref(disabled)
                    }, {
                      default: vue.withCtx(() => [
                        vue.createCommentVNode(" <ll-icon/> "),
                        _cache[5] || (_cache[5] = vue.createTextVNode(
                          " \u683C\u5F0F\u5316 ",
                          -1
                          /* CACHED */
                        ))
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled"]),
                    vue.createVNode(_component_ll_button, {
                      type: "link",
                      disabled: vue.unref(disabled)
                    }, {
                      default: vue.withCtx(() => [
                        vue.createCommentVNode(" <ll-icon/> "),
                        _cache[6] || (_cache[6] = vue.createTextVNode(
                          " \u751F\u6210\u4EE3\u7801 ",
                          -1
                          /* CACHED */
                        ))
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled"]),
                    vue.createVNode(_component_ll_dropdown, {
                      disabled: vue.unref(disabled),
                      "dropdown-items": vue.unref(jsonXml.customOverlay),
                      placement: "bottomRight"
                    }, {
                      default: vue.withCtx(() => [
                        vue.createVNode(_component_ll_icon, { "icon-name": "icon-line-arrow-down" })
                      ]),
                      _: 1
                      /* STABLE */
                    }, 8, ["disabled", "dropdown-items"])
                  ],
                  64
                  /* STABLE_FRAGMENT */
                ))
              ])
            ],
            2
            /* CLASS */
          ),
          vue.createElementVNode(
            "div",
            {
              class: vue.normalizeClass(vue.unref(bem).e("body"))
            },
            [
              type.value === "json" ? (vue.openBlock(), vue.createBlock(_component_ll_code_editor, {
                key: 0,
                value: innerValue.value,
                "onUpdate:value": _cache[1] || (_cache[1] = ($event) => innerValue.value = $event),
                disabled: vue.unref(disabled),
                style: { "height": "200px" },
                options: {
                  minimap: {
                    enabled: false
                  }
                },
                language: "json"
              }, null, 8, ["value", "disabled"])) : (vue.openBlock(), vue.createBlock(jsonXmlTable_vue_vue_type_script_setup_true_lang.default, {
                key: 1,
                type: props.type,
                "data-source": dataSource.value,
                onChange: handleChange
              }, null, 8, ["type", "data-source"]))
            ],
            2
            /* CLASS */
          )
        ],
        2
        /* CLASS */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=json-xml.vue2.js.map
