'use strict';

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

var vue = require('vue');
var objects = require('../../../../utils/src/objects.js');
var exports$1 = require('../../../../utils/src/elementPlus/exports.js');
var event = require('../../../../utils/src/elementPlus/event.js');
var table = require('../../../../constants/table.js');
var index = require('../../../../hooks/base/useElementRemainingHeight/index.js');
var TableOperateVNodeRenderer = require('../tablePro/TableOperateVNodeRenderer.js');
var tableHelper = require('../tablePro/tableHelper.js');
var tableColumnPro = require('../tablePro/tableColumnPro/tableColumnPro.js');
var index$1 = require('./customOperate/index.js');
var TableColumnVNodeRenderer = require('./TableColumnVNodeRenderer.js');
var tableCustom = require('./tableCustom.js');
var customOperate = require('./customOperate/customOperate.js');

const _withScopeId = (n) => (vue.pushScopeId("data-v-3f2c8504"), n = n(), vue.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__ */ vue.defineComponent({
  ...{
    name: "GmTableCustom"
  },
  __name: "TableCustom",
  props: /* @__PURE__ */ vue.mergeModels(tableCustom.tableCustomProps, {
    "sortValue": {},
    "sortValueModifiers": {}
  }),
  emits: /* @__PURE__ */ vue.mergeModels(tableCustom.tableCustomEmits, ["update:sortValue"]),
  setup(__props, { expose: __expose, emit: __emit }) {
    const tableCtx = vue.inject(table.TableCtxKey);
    const props = __props;
    const GmTbaleCustomRef = vue.ref();
    if (tableCtx && tableCtx.tableId === props.page) {
      tableCtx.tableRef = GmTbaleCustomRef;
    }
    const gmCustomOperateRef = vue.ref();
    const gmCustomTableHeaderRef = vue.ref();
    const emit = __emit;
    const notResendEvents = ["sort-change", "header-dragend", "selection-change"];
    const resendEvent = { ...tableCustom.tableCustomEmits };
    notResendEvents.forEach((it) => Reflect.deleteProperty(resendEvent, it));
    const elEvents = event.getGmEvent(resendEvent, emit);
    const slots = vue.useSlots();
    const tableColumnVnode = vue.computed(() => {
      const vNodes = slots.default?.() || [];
      return vNodes;
    });
    const colDefaultPropsList = vue.computed(() => {
      const vNodes = slots.default?.() || [];
      const children = tableHelper.getVnodesByGmName(vNodes, tableColumnPro.TableColumnProName);
      const colProps = children.map((vNode) => vNode.props) ?? [];
      colProps.forEach((it) => {
        for (const key in tableColumnPro.tableColumnProProps) {
          if (objects.hasOwn(it, key)) {
            continue;
          }
          if (objects.hasOwn(tableColumnPro.tableColumnProProps, key)) {
            const element = tableColumnPro.tableColumnProProps[key];
            if (objects.hasOwn(element, "default")) {
              Reflect.set(it, key, Reflect.get(element, "default"));
            }
          }
        }
      });
      return colProps;
    });
    const colPropsList = vue.ref(colDefaultPropsList.value);
    const editProps = vue.computed(() => colPropsList.value.find((it) => it.type === "edit"));
    const tableOperateVnode = vue.computed(() => {
      const vNodes = slots.default?.() || [];
      const nodes = vNodes.find((vNode) => vNode.props && vNode.props.type === "edit");
      return nodes;
    });
    const customDefaultPropsList = vue.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 = tableHelper.getVnodesByGmName(defaultVnodes, "GmOperateButton");
      const btnProps = children.map((vNode) => vNode.props) ?? [];
      btnProps.forEach((it) => {
        for (const key in customOperate.customOperateButtonProps) {
          if (objects.hasOwn(it, key)) {
            continue;
          }
          if (objects.hasOwn(customOperate.customOperateButtonProps, key)) {
            const element = customOperate.customOperateButtonProps[key];
            if (objects.hasOwn(element, "default")) {
              Reflect.set(it, key, Reflect.get(element, "default"));
            }
          }
        }
      });
      return btnProps;
    });
    const customBtnPropsList = vue.ref(customDefaultPropsList.value);
    const drawerHeaderVisible = vue.ref(false);
    const drawerOperateVisible = vue.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 vue.nextTick();
      resetWidthOnHeaderDrag(column);
      gmCustomTableHeaderRef.value?.syncPropsList();
      gmCustomTableHeaderRef.value?.submitSettings({ hiddenMessage: true });
    };
    const resetWidthOnHeaderDrag = (column) => {
      if (props.disabledResetWidth) {
        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 = vue.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 } = exports$1.getGmExports(GmTbaleCustomRef, exposeEvents);
    const gmTableCustomDivRef = vue.ref();
    const { height: tableMaxHeight } = index.useElementRemainingHeight(gmTableCustomDivRef, {
      bottomOffset: props.heightOffset || 30,
      // 高度 - 顶部距离 - 分页器
      minHeight: 300
    });
    __expose({
      colPropsList,
      ...exposeFns
    });
    return (_ctx, _cache) => {
      const _component_gm_table_column = vue.resolveComponent("gm-table-column");
      const _component_gm_button = vue.resolveComponent("gm-button");
      const _component_gm_table = vue.resolveComponent("gm-table");
      const _component_gm_table_custom_header = vue.resolveComponent("gm-table-custom-header");
      return vue.openBlock(), vue.createElementBlock(
        vue.Fragment,
        null,
        [
          vue.createElementVNode(
            "div",
            {
              ref_key: "gmTableCustomDivRef",
              ref: gmTableCustomDivRef
            },
            [
              gmCustomTableHeaderRef.value?.isInited ? (vue.openBlock(), vue.createBlock(_component_gm_table, vue.mergeProps({
                key: 0,
                ref_key: "GmTbaleCustomRef",
                ref: GmTbaleCustomRef
              }, props, {
                height: _ctx.autoHeight ? vue.unref(tableMaxHeight) : _ctx.height,
                class: ["gmTableCustom", [props.border ? "" : "gmTableNoBorder"]]
              }, vue.toHandlers(vue.unref(elEvents)), {
                onSelectionChange: _cache[2] || (_cache[2] = (value) => _ctx.$emit("selection-change", value)),
                onSortChange: handleSortChange,
                onHeaderDragend: headerDragend
              }), vue.createSlots({
                default: vue.withCtx(() => [
                  vue.renderSlot(_ctx.$slots, "native", {}, void 0, true),
                  props.selection ? (vue.openBlock(), vue.createBlock(_component_gm_table_column, {
                    key: 999999,
                    type: "selection",
                    width: "55"
                  })) : vue.createCommentVNode("v-if", true),
                  vue.createVNode(vue.unref(TableColumnVNodeRenderer.default), {
                    vnode: tableColumnVnode.value,
                    propsList: colPropsList.value,
                    defaultPropsList: colDefaultPropsList.value
                  }, null, 8, ["vnode", "propsList", "defaultPropsList"]),
                  _ctx.customHeader || _ctx.customOperate ? (vue.openBlock(), vue.createBlock(_component_gm_table_column, {
                    key: 1,
                    prop: "gmEdit",
                    "min-width": "100",
                    width: editProps.value?.width,
                    fixed: _ctx.operateFixed ? "right" : false
                  }, {
                    header: vue.withCtx(() => [
                      vue.createElementVNode("div", _hoisted_1, [
                        _ctx.customHeader ? (vue.openBlock(), vue.createBlock(_component_gm_button, {
                          key: 0,
                          type: "primary",
                          link: "",
                          size: "small",
                          onClick: _cache[0] || (_cache[0] = ($event) => setTable("header"))
                        }, {
                          default: vue.withCtx(() => [
                            vue.createTextVNode(" \u8868\u5934 ")
                          ]),
                          _: 1
                          /* STABLE */
                        })) : vue.createCommentVNode("v-if", true),
                        _ctx.customOperate ? (vue.openBlock(), vue.createBlock(_component_gm_button, {
                          key: 1,
                          type: "primary",
                          link: "",
                          size: "small",
                          onClick: _cache[1] || (_cache[1] = ($event) => setTable("operate"))
                        }, {
                          default: vue.withCtx(() => [
                            vue.createTextVNode(" \u64CD\u4F5C ")
                          ]),
                          _: 1
                          /* STABLE */
                        })) : vue.createCommentVNode("v-if", true)
                      ])
                    ]),
                    default: vue.withCtx((slotValue) => [
                      tableOperateVnode.value && tableOperateVnode.value.children && gmCustomOperateRef.value && gmCustomOperateRef.value.isInited && slotValue && slotValue.row && tableOperateVnode.value.children?.default(slotValue) && customBtnPropsList.value ? (vue.openBlock(), vue.createElementBlock("div", _hoisted_2, [
                        vue.createVNode(vue.unref(TableOperateVNodeRenderer.default), {
                          vnode: tableOperateVnode.value.children?.default(slotValue),
                          customPropsList: customBtnPropsList.value
                        }, null, 8, ["vnode", "customPropsList"])
                      ])) : vue.createCommentVNode("v-if", true)
                    ]),
                    _: 1
                    /* STABLE */
                  }, 8, ["width", "fixed"])) : vue.createCommentVNode("v-if", true)
                ]),
                _: 2
                /* DYNAMIC */
              }, [
                vue.unref(slots).append ? {
                  name: "append",
                  fn: vue.withCtx(() => [
                    vue.renderSlot(_ctx.$slots, "append", {}, void 0, true)
                  ]),
                  key: "0"
                } : void 0,
                vue.unref(slots).empty ? {
                  name: "empty",
                  fn: vue.withCtx(() => [
                    vue.renderSlot(_ctx.$slots, "empty", {}, void 0, true)
                  ]),
                  key: "1"
                } : void 0
              ]), 1040, ["height", "class"])) : vue.createCommentVNode("v-if", true)
            ],
            512
            /* NEED_PATCH */
          ),
          vue.createVNode(_component_gm_table_custom_header, {
            ref_key: "gmCustomTableHeaderRef",
            ref: gmCustomTableHeaderRef,
            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,
            "drag-header": props.dragHeader,
            type: "CustomHeaderList",
            onUpdateColumns: _cache[5] || (_cache[5] = ($event) => resetWidthOnHeaderDrag())
          }, null, 8, ["isShowDrawer", "colPropsList", "colDefaultPropsList", "keyWord", "disabledRemoteCol", "drag-header"]),
          vue.createVNode(vue.unref(index$1.GmCustomOperate), {
            ref_key: "gmCustomOperateRef",
            ref: gmCustomOperateRef,
            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: "CustomOperateList"
          }, null, 8, ["isShowDrawer", "customBtnPropsList", "customDefaultPropsList", "keyWord", "disabledRemoteBtn"])
        ],
        64
        /* STABLE_FRAGMENT */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=TableCustom.vue2.js.map
