'use strict';

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

var vue = require('vue');
var JsonEditorVue = require('json-editor-vue3');
var file = require('../../utils/file.js');
var index = require('../../button/index.js');
var index$2 = require('../../dialog/index.js');
var index$1 = require('../../svg-icon/index.js');

"use strict";
const _hoisted_1 = { class: "me-load-json" };
const _hoisted_2 = { class: "me-load-json__editor" };
const _hoisted_3 = { class: "flex-between" };
const _hoisted_4 = { style: { "display": "flex", "align-items": "center" } };
const __default__ = vue.defineComponent({
  name: "me-load-json"
});
var script = /* @__PURE__ */ vue.defineComponent({
  ...__default__,
  props: {
    data: { type: [Object, null], required: false },
    title: { type: String, required: false, default: "\u5BFC\u5165json" }
  },
  emits: ["change"],
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emits = __emit;
    const visible = vue.ref(false);
    const jsonData = vue.ref(null);
    const isJsonError = vue.ref(false);
    const handleImport = async () => {
      file.chooseFile({
        accept: ".json",
        multiple: false
      }).then((file) => {
        const reader = new FileReader();
        reader.readAsText(file, "utf-8");
        reader.onload = async () => {
          let data = [];
          try {
            const json = reader.result;
            data = JSON.parse(json);
            jsonData.value = data;
            console.log("\u{1F680} ~ reader.onload ~ data:", data);
          } catch (error) {
          }
        };
      });
    };
    const handleJsonError = (editor, error) => {
      isJsonError.value = error.length > 0 ? true : false;
    };
    const resultData = vue.ref();
    const submit = async () => {
      if (!jsonData.value) {
        return;
      }
      try {
        const json = typeof jsonData.value == "string" ? JSON.parse(jsonData.value) : jsonData.value;
        if (Array.isArray(json)) {
          const result = [];
          json.forEach(async (item) => {
            result.push(item);
          });
          resultData.value = result;
          emits("change", result);
        } else {
          resultData.value = json;
          emits("change", json);
        }
        visible.value = false;
      } catch (error) {
        console.error("\u{1F680} ~ error:", error);
      }
    };
    const open = async () => {
      if (props?.data) {
        jsonData.value = props.data;
      }
      visible.value = true;
    };
    __expose({
      getData: () => resultData.value,
      open: () => open()
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", _hoisted_1, [
        vue.createCommentVNode(' <me-button @click="open" size="small" circle type="success">\r\n      <me-svg-icon name="ri-download-2-line"></me-svg-icon>\r\n    </me-button> '),
        vue.createElementVNode("div", { onClick: open }, [
          vue.renderSlot(_ctx.$slots, "default", {}, () => [
            vue.createVNode(vue.unref(index.MeButton), {
              size: "small",
              circle: "",
              type: "success"
            }, {
              default: vue.withCtx(() => [
                vue.createVNode(vue.unref(index$1.MeSvgIcon), { name: "ri-download-2-line" })
              ]),
              _: 1
              /* STABLE */
            })
          ])
        ]),
        vue.createVNode(vue.unref(index$2.MeDialog), {
          modelValue: visible.value,
          "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => visible.value = $event),
          "show-cancel-button": false,
          "min-width": 600,
          title: __props.title
        }, {
          "me-dialog-footer": vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_3, [
              vue.renderSlot(_ctx.$slots, "footer-left", {}, () => [
                vue.createVNode(vue.unref(index.MeButton), { onClick: handleImport }, {
                  default: vue.withCtx(() => [..._cache[2] || (_cache[2] = [
                    vue.createTextVNode(
                      "\u5BFC\u5165json",
                      -1
                      /* CACHED */
                    )
                  ])]),
                  _: 1
                  /* STABLE */
                })
              ]),
              vue.createElementVNode("div", _hoisted_4, [
                vue.createVNode(vue.unref(index.MeButton), {
                  type: "primary",
                  "click-handle": submit,
                  disabled: isJsonError.value
                }, {
                  default: vue.withCtx(() => [..._cache[3] || (_cache[3] = [
                    vue.createTextVNode(
                      "\u786E\u5B9A",
                      -1
                      /* CACHED */
                    )
                  ])]),
                  _: 1
                  /* STABLE */
                }, 8, ["disabled"]),
                vue.renderSlot(_ctx.$slots, "footer-right")
              ])
            ])
          ]),
          default: vue.withCtx(() => [
            vue.createElementVNode("div", _hoisted_2, [
              vue.createVNode(vue.unref(JsonEditorVue), {
                class: "editor",
                modelValue: jsonData.value,
                "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => jsonData.value = $event),
                onValidationError: handleJsonError
              }, null, 8, ["modelValue"])
            ])
          ]),
          _: 3
          /* FORWARDED */
        }, 8, ["modelValue", "title"])
      ]);
    };
  }
});

exports.default = script;
//# sourceMappingURL=load-json.vue2.js.map
