import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                                */
/* empty css                                                               */
/* empty css                                                              */
/* empty css                                                                  */
/* empty css                                                               */
/* empty css                                                               */
/* empty css                                                             */
/* empty css                                                             */
/* empty css                                                              */
/* empty css                                                               */
import { useModel, ref, watch, createBlock, openBlock, withCtx, createVNode, unref, createTextVNode, createElementBlock, createCommentVNode, createElementVNode, withKeys, isRef, Fragment, renderList, normalizeClass, withDirectives, vShow, withModifiers, toDisplayString } from "vue";
import { Plus as plus_default, Close as close_default, Check as check_default, Delete as delete_default } from "../../../../node_modules/@element-plus/icons-vue/dist/index.mjs";
import { cloneDeep, getEditConfigDataSources, setEditConfigDataSources } from "../utils/common.mjs";
import DataSource from "./dataSource.vue.mjs";
/* empty css                                                              */
/* empty css                                                                */
/* empty css            */
import _export_sfc from "../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElDialog } from "../../../../node_modules/element-plus/es/components/dialog/index.mjs";
import { ElContainer, ElAside, ElHeader, ElMain } from "../../../../node_modules/element-plus/es/components/container/index.mjs";
import { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElInput } from "../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElLink } from "../../../../node_modules/element-plus/es/components/link/index.mjs";
import { ElIcon } from "../../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
const _hoisted_1 = { key: 0 };
const _hoisted_2 = { class: "data-source-menu-item is-editing" };
const _hoisted_3 = { class: "new-item-content" };
const _hoisted_4 = { class: "edit-actions" };
const _hoisted_5 = ["onClick"];
const _hoisted_6 = { class: "data-source-content" };
const _hoisted_7 = { class: "data-source-name-wrapper" };
const _hoisted_8 = {
  key: 1,
  class: "empty-selection"
};
const _hoisted_9 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "index",
  props: {
    "modelValue": { required: true },
    "modelModifiers": {}
  },
  emits: ["update:modelValue"],
  setup(__props) {
    const dialogVisible = useModel(__props, "modelValue");
    const selectedDataSources = ref([]);
    const activeDataSourceIndex = ref(-1);
    watch(dialogVisible, (val) => {
      if (val) {
        isEditingNew.value = false;
        editingDataSourceIndex.value = -1;
        selectedDataSources.value = cloneDeep(getEditConfigDataSources());
        if (selectedDataSources.value.length > 0 && activeDataSourceIndex.value === -1) {
          activeDataSourceIndex.value = 0;
        }
      }
    });
    const isEditingNew = ref(false);
    const newDataSourceName = ref("");
    const editingDataSourceIndex = ref(-1);
    const editingDataSourceName = ref("");
    const startAddDataSource = () => {
      isEditingNew.value = true;
      newDataSourceName.value = "";
    };
    const confirmAddDataSource = () => {
      if (!newDataSourceName.value.trim()) {
        ElMessage.warning("请输入数据源名称");
        return;
      }
      selectedDataSources.value.push({
        id: Date.now(),
        name: newDataSourceName.value,
        serverName: "",
        url: "",
        method: "post",
        contentType: "json",
        params: []
      });
      isEditingNew.value = false;
      newDataSourceName.value = "";
      activeDataSourceIndex.value = selectedDataSources.value.length - 1;
    };
    const cancelAddDataSource = () => {
      isEditingNew.value = false;
      newDataSourceName.value = "";
    };
    const handleRemoveDataSource = (index) => {
      selectedDataSources.value.splice(index, 1);
      if (activeDataSourceIndex.value === index) {
        activeDataSourceIndex.value = -1;
      } else if (activeDataSourceIndex.value > index) {
        activeDataSourceIndex.value--;
      }
    };
    const startEditDataSource = (index) => {
      editingDataSourceIndex.value = index;
      editingDataSourceName.value = selectedDataSources.value[index].name;
    };
    const onEditOutside = () => {
      if (editingDataSourceIndex.value !== -1) {
        if (!editingDataSourceName.value.trim()) {
          ElMessage.warning("请输入数据源名称");
          return;
        }
        selectedDataSources.value[editingDataSourceIndex.value].name = editingDataSourceName.value;
        editingDataSourceIndex.value = -1;
        editingDataSourceName.value = "";
      }
    };
    const onConfirm = () => {
      setEditConfigDataSources(selectedDataSources.value);
      dialogVisible.value = false;
    };
    return (_ctx, _cache) => {
      const _component_el_button = ElButton;
      const _component_el_header = ElHeader;
      const _component_el_input = ElInput;
      const _component_el_link = ElLink;
      const _component_el_icon = ElIcon;
      const _component_el_main = ElMain;
      const _component_el_container = ElContainer;
      const _component_el_aside = ElAside;
      const _component_el_dialog = ElDialog;
      return openBlock(), createBlock(_component_el_dialog, {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => dialogVisible.value = $event),
        width: "50%",
        title: "数据源配置",
        draggable: "",
        overflow: "",
        "append-to-body": "",
        class: "fec-dev-designer-dialog"
      }, {
        footer: withCtx(() => [
          createElementVNode("div", _hoisted_9, [
            createVNode(_component_el_button, {
              onClick: _cache[2] || (_cache[2] = ($event) => dialogVisible.value = false)
            }, {
              default: withCtx(() => _cache[7] || (_cache[7] = [
                createTextVNode("取消")
              ])),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              onClick: onConfirm
            }, {
              default: withCtx(() => _cache[8] || (_cache[8] = [
                createTextVNode("确定")
              ])),
              _: 1
            })
          ])
        ]),
        default: withCtx(() => [
          createVNode(_component_el_container, { style: { "border": "1px solid #ebeef5", "border-radius": "4px" } }, {
            default: withCtx(() => [
              createVNode(_component_el_aside, { width: "300px" }, {
                default: withCtx(() => [
                  createVNode(_component_el_container, { style: { "height": "600px" } }, {
                    default: withCtx(() => [
                      createVNode(_component_el_header, {
                        height: "40px",
                        style: { "background-color": "#f2f2f2", "line-height": "40px" }
                      }, {
                        default: withCtx(() => [
                          createVNode(_component_el_button, {
                            type: "primary",
                            link: "",
                            icon: unref(plus_default),
                            onClick: startAddDataSource
                          }, {
                            default: withCtx(() => _cache[4] || (_cache[4] = [
                              createTextVNode(" 新增数据源 ")
                            ])),
                            _: 1
                          }, 8, ["icon"])
                        ]),
                        _: 1
                      }),
                      createVNode(_component_el_main, { style: { "padding": "10px" } }, {
                        default: withCtx(() => [
                          unref(isEditingNew) ? (openBlock(), createElementBlock("div", _hoisted_1, [
                            createElementVNode("div", _hoisted_2, [
                              createElementVNode("div", _hoisted_3, [
                                createVNode(_component_el_input, {
                                  modelValue: unref(newDataSourceName),
                                  "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(newDataSourceName) ? newDataSourceName.value = $event : null),
                                  placeholder: "请输入数据源名称",
                                  size: "default",
                                  onKeyup: withKeys(confirmAddDataSource, ["enter"]),
                                  class: "new-item-input"
                                }, null, 8, ["modelValue"]),
                                createElementVNode("div", _hoisted_4, [
                                  createVNode(_component_el_button, {
                                    size: "small",
                                    icon: unref(close_default),
                                    onClick: cancelAddDataSource
                                  }, {
                                    default: withCtx(() => _cache[5] || (_cache[5] = [
                                      createTextVNode(" 取消 ")
                                    ])),
                                    _: 1
                                  }, 8, ["icon"]),
                                  createVNode(_component_el_button, {
                                    type: "primary",
                                    size: "small",
                                    icon: unref(check_default),
                                    onClick: confirmAddDataSource
                                  }, {
                                    default: withCtx(() => _cache[6] || (_cache[6] = [
                                      createTextVNode(" 确认 ")
                                    ])),
                                    _: 1
                                  }, 8, ["icon"])
                                ])
                              ])
                            ])
                          ])) : createCommentVNode("", true),
                          (openBlock(true), createElementBlock(Fragment, null, renderList(unref(selectedDataSources), (dataSource, index) => {
                            return openBlock(), createElementBlock("div", {
                              key: index,
                              class: normalizeClass([
                                "data-source-menu-item",
                                { "is-active": unref(activeDataSourceIndex) === index }
                              ]),
                              onClick: ($event) => activeDataSourceIndex.value = index
                            }, [
                              createElementVNode("div", _hoisted_6, [
                                createElementVNode("div", _hoisted_7, [
                                  withDirectives(createVNode(_component_el_input, {
                                    onBlur: ($event) => onEditOutside(),
                                    maxlength: "30",
                                    modelValue: unref(editingDataSourceName),
                                    "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(editingDataSourceName) ? editingDataSourceName.value = $event : null),
                                    size: "small"
                                  }, null, 8, ["onBlur", "modelValue"]), [
                                    [vShow, unref(editingDataSourceIndex) === index]
                                  ]),
                                  withDirectives(createVNode(_component_el_link, {
                                    icon: "Edit",
                                    onClick: withModifiers(($event) => startEditDataSource(index), ["stop"])
                                  }, {
                                    default: withCtx(() => [
                                      createTextVNode(toDisplayString(dataSource.name), 1)
                                    ]),
                                    _: 2
                                  }, 1032, ["onClick"]), [
                                    [vShow, unref(editingDataSourceIndex) !== index]
                                  ])
                                ]),
                                createVNode(_component_el_icon, {
                                  class: "delete-icon",
                                  onClick: withModifiers(($event) => handleRemoveDataSource(index), ["stop"])
                                }, {
                                  default: withCtx(() => [
                                    createVNode(unref(delete_default))
                                  ]),
                                  _: 2
                                }, 1032, ["onClick"])
                              ])
                            ], 10, _hoisted_5);
                          }), 128))
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }),
              createVNode(_component_el_main, { style: { "border-left": "1px solid #ebeef5", "padding": "0" } }, {
                default: withCtx(() => [
                  unref(activeDataSourceIndex) !== -1 ? (openBlock(), createBlock(DataSource, {
                    key: 0,
                    requestConfigData: unref(selectedDataSources)[unref(activeDataSourceIndex)]
                  }, null, 8, ["requestConfigData"])) : (openBlock(), createElementBlock("div", _hoisted_8, "请从左侧选择一个数据源"))
                ]),
                _: 1
              })
            ]),
            _: 1
          })
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
};
const DataSourceDialog = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-5fe0fbb7"]]);
export {
  DataSourceDialog as default
};
