import { pushScopeId, popScopeId, defineComponent, inject, ref, computed, useSlots, nextTick, useModel, resolveComponent, createElementBlock, openBlock, Fragment, createElementVNode, createVNode, createBlock, createCommentVNode, mergeProps, toHandlers, unref, createSlots, withCtx, renderSlot, createTextVNode, mergeModels } from 'vue';
import { hasOwn } from '../../../../utils/src/objects.mjs';
import { getGmExports } from '../../../../utils/src/elementPlus/exports.mjs';
import { getGmEvent } from '../../../../utils/src/elementPlus/event.mjs';
import { TableCtxKey } from '../../../../constants/table.mjs';
import { useElementBounding, useWindowSize } from '@vueuse/core';
import { operateButtonProps } from '../operateButton/operateButton.mjs';
import TableOperateVNodeRenderer from './TableOperateVNodeRenderer.mjs';
import TableColumnVNodeRenderer from './TableColumnVNodeRenderer.mjs';
import { tableProEmits, tableProProps } from './tablePro.mjs';
import { getVnodesByGmName } from './tableHelper.mjs';
import { TableColumnProName, tableColumnProProps } from './tableColumnPro/tableColumnPro.mjs';

const _withScopeId = (n) => (pushScopeId("data-v-050a4f74"), n = n(), popScopeId(), n);
const _hoisted_1 = { class: "gm-text-center" };
const _hoisted_2 = {
  key: 0,
  class: "gm-flex gm-flex-wrap gm-justify-center"
};
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "GmTablePro"
  },
  __name: "TablePro",
  props: /* @__PURE__ */ mergeModels(tableProProps, {
    "sortValue": {},
    "sortValueModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(tableProEmits, ["update:sortValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const tableCtx = inject(TableCtxKey);
    const props = __props;
    const gmTableDivRef = ref();
    const { top: divTop } = useElementBounding(gmTableDivRef);
    const { height: windowHeight } = useWindowSize();
    const tableMaxHeight = computed(() => Math.floor(windowHeight.value - divTop.value - 60 - props.heightOffset));
    const GmTbaleRef = ref();
    if (tableCtx && tableCtx.tableId === props.page) {
      tableCtx.tableRef = GmTbaleRef;
    }
    const gmCustomOperationsRef = ref();
    const gmTableHeaderRef = ref();
    const emit = __emit;
    const notResendEvents = ["sort-change", "header-dragend", "selection-change"];
    const resendEvent = { ...tableProEmits };
    notResendEvents.forEach((it) => Reflect.deleteProperty(resendEvent, it));
    const elEvents = getGmEvent(resendEvent, emit);
    const slots = useSlots();
    const tableColumnVnode = computed(() => {
      const vNodes = slots.default?.() || [];
      return vNodes;
    });
    const colDefaultPropsList = computed(() => {
      const vNodes = slots.default?.() || [];
      const children = getVnodesByGmName(vNodes, TableColumnProName, true);
      const colProps = children.map((vNode) => vNode.props) ?? [];
      colProps.forEach((it) => {
        for (const key in tableColumnProProps) {
          if (hasOwn(it, key)) {
            continue;
          }
          if (hasOwn(tableColumnProProps, key)) {
            const element = tableColumnProProps[key];
            if (hasOwn(element, "default")) {
              Reflect.set(it, key, Reflect.get(element, "default"));
            }
          }
        }
      });
      return colProps;
    });
    const colPropsList = ref(colDefaultPropsList.value);
    const editProps = computed(() => colPropsList.value.find((it) => it.type === "edit"));
    const tableOperateVnode = computed(() => {
      const vNodes = slots.default?.() || [];
      const nodes = vNodes.find((vNode) => vNode.props && vNode.props.type === "edit");
      return nodes;
    });
    const customDefaultPropsList = computed(() => {
      const defaultVnodes = tableOperateVnode.value?.children?.default?.({ row: {} });
      if (Array.isArray(defaultVnodes) && defaultVnodes.some((it) => it?.children === "v-if")) {
        console.error(`\u8B66\u544A\uFF1A\u4E3A\u786E\u4FDD\u8868\u683C\u64CD\u4F5C\u6309\u94AE\u80FD\u6B63\u5E38\u6E32\u67D3\uFF0C\u8BF7\u628A\u8868\u683C${props.page}\u64CD\u4F5C\u6309\u94AE\u4E2D\u4F7F\u7528\u7684"v-if"\u66FF\u6362\u4E3A"v-see"`);
      }
      const children = getVnodesByGmName(defaultVnodes, "GmOperateButton");
      const btnProps = children.map((vNode) => vNode.props) ?? [];
      btnProps.forEach((it) => {
        for (const key in operateButtonProps) {
          if (hasOwn(it, key)) {
            continue;
          }
          if (hasOwn(operateButtonProps, key)) {
            const element = operateButtonProps[key];
            if (hasOwn(element, "default")) {
              Reflect.set(it, key, Reflect.get(element, "default"));
            }
          }
        }
      });
      return btnProps;
    });
    const customBtnPropsList = ref(customDefaultPropsList.value);
    const drawerHeaderVisible = ref(false);
    const drawerOperateVisible = ref(false);
    const setTable = (event) => {
      if (event === "header") {
        drawerHeaderVisible.value = true;
      }
      if (event === "operate") {
        drawerOperateVisible.value = true;
      }
    };
    const headerDragend = async (newWidth, oldWidth, column) => {
      const findItem = colPropsList.value.find((it) => it.prop === column.property);
      const editItem = column.property === "gmEdit" ? colPropsList.value.find((it) => it.type === "edit") : null;
      if (!findItem && !editItem) {
        return;
      }
      if (findItem) {
        findItem.width = newWidth;
      }
      if (editItem) {
        editItem.width = newWidth;
      }
      await nextTick();
      resetWidthOnHeaderDrag(column);
      gmTableHeaderRef.value?.syncPropsList();
      gmTableHeaderRef.value?.submitSettings({ hiddenMessage: true });
    };
    const resetWidthOnHeaderDrag = (column) => {
      if (props.disabledResetWidth) {
        return;
      }
      if (colPropsList.value.some((it) => it.isShow && !it.width)) {
        return;
      }
      const minWidthCols = colPropsList.value.filter(
        // 是在展示中的列，并且不是操作列，并且设置了最小宽度
        (it) => it.isShow === true && it.type !== "edit" && it.prop !== "edit" && (it["min-width"] || it.minWidth)
      );
      for (const item of minWidthCols) {
        if (column?.property === item.prop && minWidthCols.length !== 1) {
          continue;
        }
        item.width = "";
      }
    };
    const sortValue = useModel(__props, "sortValue");
    const handleSortChange = (sort) => {
      if (sortValue.value) {
        sortValue.value.length = 0;
        switch (sort.order) {
          case "ascending":
            sortValue.value.push({ field: sort.prop, order: "asc" });
            break;
          case "descending":
            sortValue.value.push({ field: sort.prop, order: "desc" });
            break;
        }
      }
      emit("sort-change", sort);
    };
    const exposeEvents = [
      "clearSelection",
      "getSelectionRows",
      "toggleRowSelection",
      "toggleAllSelection",
      "toggleRowExpansion",
      "setCurrentRow",
      "setChecked",
      "clearSort",
      "clearFilter",
      "doLayout",
      "sort",
      "scrollTo",
      "setScrollTop",
      "setScrollLeft"
    ];
    const { exposeFns } = getGmExports(GmTbaleRef, exposeEvents);
    __expose({
      colPropsList,
      ...exposeFns
    });
    return (_ctx, _cache) => {
      const _component_gm_table_column = resolveComponent("gm-table-column");
      const _component_gm_button = resolveComponent("gm-button");
      const _component_gm_table = resolveComponent("gm-table");
      const _component_gm_table_header_form = resolveComponent("gm-table-header-form");
      const _component_gm_operate_button_form = resolveComponent("gm-operate-button-form");
      return openBlock(), createElementBlock(
        Fragment,
        null,
        [
          createElementVNode(
            "div",
            {
              ref_key: "gmTableDivRef",
              ref: gmTableDivRef
            },
            [
              gmTableHeaderRef.value?.isInited ? (openBlock(), createBlock(_component_gm_table, mergeProps({
                key: 0,
                ref_key: "GmTbaleRef",
                ref: GmTbaleRef
              }, props, {
                height: _ctx.autoHeight ? tableMaxHeight.value : _ctx.height,
                border: "",
                class: ["gmTablePro", [props.border ? "" : "gmTableNoBorder"]]
              }, toHandlers(unref(elEvents)), {
                onSelectionChange: _cache[2] || (_cache[2] = (value) => _ctx.$emit("selection-change", value)),
                onSortChange: handleSortChange,
                onHeaderDragend: headerDragend
              }), createSlots({
                default: withCtx(() => [
                  renderSlot(_ctx.$slots, "native", {}, void 0, true),
                  props.selection ? (openBlock(), createBlock(_component_gm_table_column, {
                    key: 999999,
                    type: "selection",
                    width: "55"
                  })) : createCommentVNode("v-if", true),
                  createVNode(unref(TableColumnVNodeRenderer), {
                    vnode: tableColumnVnode.value,
                    propsList: colPropsList.value,
                    defaultPropsList: colDefaultPropsList.value
                  }, null, 8, ["vnode", "propsList", "defaultPropsList"]),
                  _ctx.customHeader || _ctx.customOperate ? (openBlock(), createBlock(_component_gm_table_column, {
                    key: 1,
                    prop: "gmEdit",
                    "min-width": "100",
                    width: editProps.value?.width,
                    fixed: _ctx.operateFixed ? "right" : false
                  }, {
                    header: withCtx(() => [
                      createElementVNode("div", _hoisted_1, [
                        _ctx.customHeader ? (openBlock(), createBlock(_component_gm_button, {
                          key: 0,
                          type: "primary",
                          link: "",
                          size: "small",
                          onClick: _cache[0] || (_cache[0] = ($event) => setTable("header"))
                        }, {
                          default: withCtx(() => [
                            createTextVNode(" \u8868\u5934 ")
                          ]),
                          _: 1
                          /* STABLE */
                        })) : createCommentVNode("v-if", true),
                        _ctx.customOperate ? (openBlock(), createBlock(_component_gm_button, {
                          key: 1,
                          type: "primary",
                          link: "",
                          size: "small",
                          onClick: _cache[1] || (_cache[1] = ($event) => setTable("operate"))
                        }, {
                          default: withCtx(() => [
                            createTextVNode(" \u64CD\u4F5C ")
                          ]),
                          _: 1
                          /* STABLE */
                        })) : createCommentVNode("v-if", true)
                      ])
                    ]),
                    default: withCtx((slotValue) => [
                      createCommentVNode(' <div\n            v-if="\n              tableOperateVnode &&\n              tableOperateVnode.children &&\n              gmCustomOperationsRef &&\n              gmCustomOperationsRef.isInited &&\n              slotValue &&\n              slotValue.row &&\n              tableOperateVnode.children?.default(slotValue) &&\n              customBtnPropsList\n            "\n          >\n            <TableProEditSlot\n              v-model:customBtnPropsList="customBtnPropsList"\n              v-model:customDefaultPropsList="customDefaultPropsList"\n              :tableOperateVnode="tableOperateVnode"\n              :isInited="gmCustomOperationsRef?.isInited"\n              :slotValue="slotValue"\n            />\n          </div> '),
                      tableOperateVnode.value && tableOperateVnode.value.children && gmCustomOperationsRef.value && gmCustomOperationsRef.value.isInited && slotValue && slotValue.row && tableOperateVnode.value.children?.default(slotValue) && customBtnPropsList.value ? (openBlock(), createElementBlock("div", _hoisted_2, [
                        createVNode(unref(TableOperateVNodeRenderer), {
                          vnode: tableOperateVnode.value.children?.default(slotValue),
                          customPropsList: customBtnPropsList.value
                        }, null, 8, ["vnode", "customPropsList"])
                      ])) : createCommentVNode("v-if", true)
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["width", "fixed"])) : createCommentVNode("v-if", true)
                ]),
                _: 2
                /* DYNAMIC */
              }, [
                unref(slots).append ? {
                  name: "append",
                  fn: withCtx(() => [
                    renderSlot(_ctx.$slots, "append", {}, void 0, true)
                  ]),
                  key: "0"
                } : void 0,
                unref(slots).empty ? {
                  name: "empty",
                  fn: withCtx(() => [
                    renderSlot(_ctx.$slots, "empty", {}, void 0, true)
                  ]),
                  key: "1"
                } : void 0
              ]), 1040, ["height", "class"])) : createCommentVNode("v-if", true)
            ],
            512
            /* NEED_PATCH */
          ),
          createVNode(_component_gm_table_header_form, {
            ref_key: "gmTableHeaderRef",
            ref: gmTableHeaderRef,
            isShowDrawer: drawerHeaderVisible.value,
            "onUpdate:isShowDrawer": _cache[3] || (_cache[3] = ($event) => drawerHeaderVisible.value = $event),
            colPropsList: colPropsList.value,
            "onUpdate:colPropsList": _cache[4] || (_cache[4] = ($event) => colPropsList.value = $event),
            colDefaultPropsList: colDefaultPropsList.value,
            keyWord: props.page,
            disabledRemoteCol: props.disabledRemoteCol,
            type: "HeaderList",
            onUpdateColumns: _cache[5] || (_cache[5] = ($event) => resetWidthOnHeaderDrag())
          }, null, 8, ["isShowDrawer", "colPropsList", "colDefaultPropsList", "keyWord", "disabledRemoteCol"]),
          createVNode(_component_gm_operate_button_form, {
            ref_key: "gmCustomOperationsRef",
            ref: gmCustomOperationsRef,
            isShowDrawer: drawerOperateVisible.value,
            "onUpdate:isShowDrawer": _cache[6] || (_cache[6] = ($event) => drawerOperateVisible.value = $event),
            customBtnPropsList: customBtnPropsList.value,
            "onUpdate:customBtnPropsList": _cache[7] || (_cache[7] = ($event) => customBtnPropsList.value = $event),
            customDefaultPropsList: customDefaultPropsList.value,
            keyWord: props.page,
            disabledRemoteBtn: props.disabledRemoteBtn,
            type: "OperateList"
          }, null, 8, ["isShowDrawer", "customBtnPropsList", "customDefaultPropsList", "keyWord", "disabledRemoteBtn"])
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

export { _sfc_main as default };
//# sourceMappingURL=TablePro.vue2.mjs.map
