import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                                */
/* empty css                                                               */
/* empty css                                                               */
/* empty css                                                             */
import "../../../../node_modules/element-plus/theme-chalk/el-tab-pane.css.mjs";
/* empty css                                                              */
/* empty css                                                                  */
/* empty css                                                               */
/* empty css                                                               */
/* empty css                                                             */
/* empty css                                                                  */
/* empty css                                                               */
/* empty css                                                                     */
/* empty css                                                                 */
import "../../../../node_modules/element-plus/theme-chalk/el-dropdown-menu.css.mjs";
import "../../../../node_modules/element-plus/theme-chalk/el-dropdown-item.css.mjs";
import { mergeModels, useModel, ref, watch, computed, resolveComponent, createBlock, openBlock, withCtx, createVNode, createElementVNode, createTextVNode, createElementBlock, Fragment, renderList, toDisplayString, normalizeClass, normalizeStyle } from "vue";
import _sfc_main$1 from "../eventFlow/index.vue.mjs";
import defaultProcessData from "../eventFlow/defaultProcessData.mjs";
import { cloneDeep } from "../utils/common.mjs";
/* 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 { ElDropdown, ElDropdownMenu, ElDropdownItem } from "../../../../node_modules/element-plus/es/components/dropdown/index.mjs";
import { ElIcon } from "../../../../node_modules/element-plus/es/components/icon/index.mjs";
import { ElTabs, ElTabPane } from "../../../../node_modules/element-plus/es/components/tabs/index.mjs";
import { ElButton } from "../../../../node_modules/element-plus/es/components/button/index.mjs";
import { ElMessage } from "../../../../node_modules/element-plus/es/components/message/index.mjs";
const _hoisted_1 = { class: "el-dropdown-link" };
const _hoisted_2 = ["onClick"];
const _hoisted_3 = { class: "event-title" };
const _hoisted_4 = { class: "event-method" };
const _hoisted_5 = { class: "el-dropdown-link" };
const _hoisted_6 = { class: "event-name" };
const _hoisted_7 = { class: "event-label" };
const _hoisted_8 = { class: "tab-content" };
const _hoisted_9 = {
  key: 1,
  class: "empty-selection"
};
const _hoisted_10 = { class: "dialog-footer" };
const _sfc_main = {
  __name: "index",
  props: /* @__PURE__ */ mergeModels({
    eventConfig: Object,
    title: {
      type: String,
      default: "事件配置"
    },
    onConfirm: Function
  }, {
    "modelValue": { required: true },
    "modelModifiers": {}
  }),
  emits: ["update:modelValue"],
  setup(__props) {
    const dialogVisible = useModel(__props, "modelValue");
    const props = __props;
    const createEventFlow = () => JSON.parse(JSON.stringify(defaultProcessData));
    const eventData = ref([
      { name: "Click", description: "点击时触发", eventFlow: createEventFlow() },
      {
        name: "Change",
        description: "当绑定值变化时触发",
        eventFlow: createEventFlow()
      },
      {
        name: "Input",
        description: "当绑定值变化时触发",
        eventFlow: createEventFlow()
      },
      { name: "Blur", description: "失去焦点时触发", eventFlow: createEventFlow() },
      {
        name: "Focus",
        description: "获得焦点时触发",
        eventFlow: createEventFlow()
      },
      {
        name: "Clear",
        description: "在点击清空按钮时触发",
        eventFlow: createEventFlow()
      }
    ]);
    const selectedEvents = ref([]);
    const activeEventIndex = ref(-1);
    watch(dialogVisible, (val) => {
      if (val) {
        selectedEvents.value = cloneDeep(props.eventConfig) || [];
        if (selectedEvents.value.length > 0) {
          activeEventIndex.value = 0;
        }
      }
    });
    const activeTab = ref("eventFlow");
    const eventFlowRefs = ref({});
    const availableEventTypes = computed(() => {
      return eventData.value;
    });
    const handleChangeEventType = (index, newEventName) => {
      const eventType = eventData.value.find((e) => e.name === newEventName);
      if (eventType) {
        const isAlreadySelected = selectedEvents.value.some(
          (event, i) => i !== index && event.name === newEventName
        );
        if (isAlreadySelected) {
          ElMessage.warning(`事件类型 "${newEventName}" 已被选择，请选择其他类型`);
          return;
        }
        const currentEvent = selectedEvents.value[index];
        const newEvent = {
          name: eventType.name,
          description: eventType.description,
          eventFlow: currentEvent.eventFlow
          // 保留原有的流程配置
        };
        selectedEvents.value.splice(index, 1, newEvent);
      }
    };
    const validationErrors = ref({});
    const setEventFlowRef = (el, index) => {
      if (el) {
        eventFlowRefs.value[index] = el;
      }
    };
    const validateAllEventFlows = async () => {
      var _a;
      validationErrors.value = {};
      const errors = [];
      for (const [index, ref2] of Object.entries(eventFlowRefs.value)) {
        try {
          if (ref2 == null ? void 0 : ref2.validate) {
            await ref2.validate();
          }
        } catch (error) {
          validationErrors.value[index] = true;
          errors.push({
            event: (_a = selectedEvents.value[index]) == null ? void 0 : _a.name,
            errors: error
          });
        }
      }
      if (errors.length > 0) {
        throw errors;
      }
    };
    const isEventSelected = (eventName) => {
      return selectedEvents.value.some((e) => e.name === eventName);
    };
    const handleAddEvent = (event) => {
      const exists = selectedEvents.value.some((e) => e.name === event.name);
      if (!exists) {
        selectedEvents.value.push({
          name: event.name,
          description: event.description,
          eventFlow: createEventFlow()
        });
        activeEventIndex.value = selectedEvents.value.length - 1;
        activeTab.value = "eventFlow";
      }
    };
    const handleRemoveEvent = (index) => {
      selectedEvents.value.splice(index, 1);
      delete eventFlowRefs.value[index];
      if (activeEventIndex.value === index) {
        activeEventIndex.value = -1;
        activeTab.value = "eventFlow";
      } else if (activeEventIndex.value > index) {
        activeEventIndex.value--;
      }
    };
    const onConfirm = async () => {
      try {
        if (activeTab.value === "eventFlow") {
          await validateAllEventFlows();
        }
        props.onConfirm && props.onConfirm(cloneDeep(selectedEvents.value));
        dialogVisible.value = false;
      } catch (errors) {
        console.error("事件流校验失败:", errors);
        const eventNames = errors.map((e) => e.event).join("、");
        ElMessage.error(`请完善 ${eventNames} 的事件配置`);
      }
    };
    return (_ctx, _cache) => {
      const _component_arrow_down = resolveComponent("arrow-down");
      const _component_el_icon = ElIcon;
      const _component_el_dropdown_item = ElDropdownItem;
      const _component_el_dropdown_menu = ElDropdownMenu;
      const _component_el_dropdown = ElDropdown;
      const _component_el_header = ElHeader;
      const _component_Delete = resolveComponent("Delete");
      const _component_el_main = ElMain;
      const _component_el_container = ElContainer;
      const _component_el_aside = ElAside;
      const _component_el_tab_pane = ElTabPane;
      const _component_el_tabs = ElTabs;
      const _component_el_button = ElButton;
      const _component_el_dialog = ElDialog;
      return openBlock(), createBlock(_component_el_dialog, {
        modelValue: dialogVisible.value,
        "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => dialogVisible.value = $event),
        width: "70%",
        title: __props.title,
        draggable: "",
        overflow: "",
        "append-to-body": "",
        class: "fec-dev-designer-dialog"
      }, {
        footer: withCtx(() => [
          createElementVNode("div", _hoisted_10, [
            createVNode(_component_el_button, {
              onClick: _cache[1] || (_cache[1] = ($event) => dialogVisible.value = false)
            }, {
              default: withCtx(() => _cache[6] || (_cache[6] = [
                createTextVNode("取消")
              ])),
              _: 1
            }),
            createVNode(_component_el_button, {
              type: "primary",
              onClick: onConfirm
            }, {
              default: withCtx(() => _cache[7] || (_cache[7] = [
                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" }
                      }, {
                        default: withCtx(() => [
                          createVNode(_component_el_dropdown, {
                            "max-height": 500,
                            trigger: "click",
                            placement: "bottom-start",
                            style: { "height": "100%" }
                          }, {
                            dropdown: withCtx(() => [
                              createVNode(_component_el_dropdown_menu, null, {
                                default: withCtx(() => [
                                  (openBlock(true), createElementBlock(Fragment, null, renderList(eventData.value, (event) => {
                                    return openBlock(), createBlock(_component_el_dropdown_item, {
                                      key: event.name,
                                      disabled: isEventSelected(event.name),
                                      onClick: ($event) => handleAddEvent(event)
                                    }, {
                                      default: withCtx(() => [
                                        createElementVNode("div", null, [
                                          createElementVNode("span", null, toDisplayString(event.name), 1),
                                          createElementVNode("span", null, toDisplayString(event.description), 1)
                                        ])
                                      ]),
                                      _: 2
                                    }, 1032, ["disabled", "onClick"]);
                                  }), 128))
                                ]),
                                _: 1
                              })
                            ]),
                            default: withCtx(() => [
                              createElementVNode("span", _hoisted_1, [
                                _cache[3] || (_cache[3] = createTextVNode(" 新增事件 ")),
                                createVNode(_component_el_icon, { class: "el-icon--right" }, {
                                  default: withCtx(() => [
                                    createVNode(_component_arrow_down)
                                  ]),
                                  _: 1
                                })
                              ])
                            ]),
                            _: 1
                          })
                        ]),
                        _: 1
                      }),
                      createVNode(_component_el_main, { style: { "padding": "10px" } }, {
                        default: withCtx(() => [
                          (openBlock(true), createElementBlock(Fragment, null, renderList(selectedEvents.value, (event, index) => {
                            return openBlock(), createElementBlock("div", {
                              key: index,
                              class: normalizeClass([
                                "event-menu-item",
                                {
                                  "is-active": activeEventIndex.value === index,
                                  "is-error": validationErrors.value[index]
                                }
                              ]),
                              onClick: ($event) => activeEventIndex.value = index
                            }, [
                              createElementVNode("div", _hoisted_3, [
                                createElementVNode("div", _hoisted_4, [
                                  createVNode(_component_el_dropdown, {
                                    "max-height": 500,
                                    trigger: "click",
                                    placement: "bottom-start",
                                    onCommand: (command) => handleChangeEventType(index, command)
                                  }, {
                                    dropdown: withCtx(() => [
                                      createVNode(_component_el_dropdown_menu, null, {
                                        default: withCtx(() => [
                                          (openBlock(true), createElementBlock(Fragment, null, renderList(availableEventTypes.value, (eventType) => {
                                            return openBlock(), createBlock(_component_el_dropdown_item, {
                                              key: eventType.name,
                                              disabled: eventType.name === event.name,
                                              command: eventType.name
                                            }, {
                                              default: withCtx(() => [
                                                createElementVNode("div", null, [
                                                  createElementVNode("span", null, toDisplayString(eventType.name), 1),
                                                  createElementVNode("span", null, toDisplayString(eventType.description), 1)
                                                ])
                                              ]),
                                              _: 2
                                            }, 1032, ["disabled", "command"]);
                                          }), 128))
                                        ]),
                                        _: 2
                                      }, 1024)
                                    ]),
                                    default: withCtx(() => [
                                      createElementVNode("span", _hoisted_5, [
                                        _cache[4] || (_cache[4] = createTextVNode(" function ")),
                                        createElementVNode("span", _hoisted_6, toDisplayString(event.name), 1),
                                        createVNode(_component_el_icon, { class: "el-icon--right" }, {
                                          default: withCtx(() => [
                                            createVNode(_component_arrow_down)
                                          ]),
                                          _: 1
                                        })
                                      ])
                                    ]),
                                    _: 2
                                  }, 1032, ["onCommand"]),
                                  createElementVNode("span", _hoisted_7, toDisplayString(event.description), 1)
                                ]),
                                createVNode(_component_el_icon, {
                                  class: "delete-icon",
                                  onClick: ($event) => handleRemoveEvent(index)
                                }, {
                                  default: withCtx(() => [
                                    createVNode(_component_Delete)
                                  ]),
                                  _: 2
                                }, 1032, ["onClick"])
                              ])
                            ], 10, _hoisted_2);
                          }), 128))
                        ]),
                        _: 1
                      })
                    ]),
                    _: 1
                  })
                ]),
                _: 1
              }),
              createVNode(_component_el_main, { style: { "border-left": "1px solid #ebeef5", "padding": "0" } }, {
                default: withCtx(() => [
                  activeEventIndex.value !== -1 ? (openBlock(), createBlock(_component_el_tabs, {
                    key: 0,
                    modelValue: activeTab.value,
                    "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => activeTab.value = $event),
                    class: "event-tabs"
                  }, {
                    default: withCtx(() => [
                      createVNode(_component_el_tab_pane, {
                        label: "事件流",
                        name: "eventFlow"
                      }, {
                        default: withCtx(() => [
                          createElementVNode("div", _hoisted_8, [
                            (openBlock(true), createElementBlock(Fragment, null, renderList(selectedEvents.value, (event, index) => {
                              return openBlock(), createBlock(_sfc_main$1, {
                                key: index,
                                ref_for: true,
                                ref: (el) => setEventFlowRef(el, index),
                                process: event.eventFlow,
                                style: normalizeStyle({
                                  display: activeEventIndex.value === index ? "block" : "none"
                                })
                              }, null, 8, ["process", "style"]);
                            }), 128))
                          ])
                        ]),
                        _: 1
                      }),
                      createVNode(_component_el_tab_pane, {
                        label: "自定义",
                        name: "custom"
                      }, {
                        default: withCtx(() => _cache[5] || (_cache[5] = [
                          createElementVNode("div", { class: "tab-content" }, [
                            createElementVNode("div", { class: "custom-code-area" }, " 自定义 ")
                          ], -1)
                        ])),
                        _: 1
                      })
                    ]),
                    _: 1
                  }, 8, ["modelValue"])) : (openBlock(), createElementBlock("div", _hoisted_9, "请从左侧选择一个事件"))
                ]),
                _: 1
              })
            ]),
            _: 1
          })
        ]),
        _: 1
      }, 8, ["modelValue", "title"]);
    };
  }
};
const EventConfig = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-64aef55e"]]);
export {
  EventConfig as default
};
