"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index$1 = require("../../../../hooks/use-locale/index.js");
const index = require("../../../../hooks/use-namespace/index.js");
const watcher = require("./watcher.js");
const Vue = require("vue");
const isNull = require("../../../../../../lodash-es/isNull.js");
function replaceColumn(array, column) {
  return array.map((item) => {
    var _a;
    if (item.id === column.id) return column;
    else if ((_a = item.children) == null ? void 0 : _a.length) item.children = replaceColumn(item.children, column);
    return item;
  });
}
function sortColumn(array) {
  array.forEach((item) => {
    var _a, _b;
    item.no = (_a = item.getColumnIndex) == null ? void 0 : _a.call(item);
    if ((_b = item.children) == null ? void 0 : _b.length) sortColumn(item.children);
  });
  array.sort((cur, pre) => cur.no - pre.no);
}
function useStore() {
  const instance = Vue.getCurrentInstance();
  const watcher$1 = watcher.default();
  const ns = index.useNamespace("table");
  const { t } = index$1.useLocale();
  const mutations = {
    setData(states, data) {
      const dataInstanceChanged = Vue.unref(states._data) !== data;
      states.data.value = data;
      states._data.value = data;
      instance.store.execQuery();
      instance.store.updateCurrentRowData();
      instance.store.updateExpandRows();
      instance.store.updateTreeData(instance.store.states.defaultExpandAll.value);
      if (Vue.unref(states.reserveSelection)) instance.store.assertRowKey();
      else if (dataInstanceChanged) instance.store.clearSelection();
      else instance.store.cleanSelection();
      instance.store.updateAllSelected();
      if (instance.$ready) instance.store.scheduleLayout();
    },
    insertColumn(states, column, parent, updateColumnOrder) {
      var _a;
      const array = Vue.unref(states._columns);
      let newColumns = [];
      if (!parent) {
        array.push(column);
        newColumns = array;
      } else {
        if (parent && !parent.children) parent.children = [];
        (_a = parent.children) == null ? void 0 : _a.push(column);
        newColumns = replaceColumn(array, parent);
      }
      sortColumn(newColumns);
      states._columns.value = newColumns;
      states.updateOrderFns.push(updateColumnOrder);
      if (column.type === "selection") {
        states.selectable.value = column.selectable;
        states.reserveSelection.value = column.reserveSelection;
      }
      if (instance.$ready) {
        instance.store.updateColumns();
        instance.store.scheduleLayout();
      }
    },
    updateColumnOrder(states, column) {
      var _a;
      if (((_a = column.getColumnIndex) == null ? void 0 : _a.call(column)) === column.no) return;
      sortColumn(states._columns.value);
      if (instance.$ready) instance.store.updateColumns();
    },
    removeColumn(states, column, parent, updateColumnOrder) {
      var _a;
      const array = Vue.unref(states._columns) || [];
      if (parent) {
        (_a = parent.children) == null ? void 0 : _a.splice(parent.children.findIndex((item) => item.id === column.id), 1);
        Vue.nextTick(() => {
          var _a2;
          if (((_a2 = parent.children) == null ? void 0 : _a2.length) === 0) delete parent.children;
        });
        states._columns.value = replaceColumn(array, parent);
      } else {
        const index2 = array.indexOf(column);
        if (index2 > -1) {
          array.splice(index2, 1);
          states._columns.value = array;
        }
      }
      const updateFnIndex = states.updateOrderFns.indexOf(updateColumnOrder);
      updateFnIndex > -1 && states.updateOrderFns.splice(updateFnIndex, 1);
      if (instance.$ready) {
        instance.store.updateColumns();
        instance.store.scheduleLayout();
      }
    },
    sort(states, options) {
      const { prop, order, init } = options;
      if (prop) {
        const column = Vue.unref(states.columns).find((column2) => column2.property === prop);
        if (column) {
          column.order = order;
          instance.store.updateSort(column, prop, order);
          instance.store.commit("changeSortCondition", { init });
        }
      }
    },
    changeSortCondition(states, options) {
      const { sortingColumn, sortProp, sortOrder } = states;
      const columnValue = Vue.unref(sortingColumn), propValue = Vue.unref(sortProp), orderValue = Vue.unref(sortOrder);
      if (isNull.default(orderValue)) {
        states.sortingColumn.value = null;
        states.sortProp.value = null;
      }
      instance.store.execQuery({ filter: true });
      if (!options || !(options.silent || options.init)) instance.emit("sort-change", {
        column: columnValue,
        prop: propValue,
        order: orderValue
      });
      instance.store.updateTableScrollY();
    },
    filterChange(_states, options) {
      const { column, values, silent } = options;
      const newFilters = instance.store.updateFilters(column, values);
      instance.store.execQuery();
      if (!silent) instance.emit("filter-change", newFilters);
      instance.store.updateTableScrollY();
    },
    toggleAllSelection() {
      var _a, _b;
      (_b = (_a = instance.store).toggleAllSelection) == null ? void 0 : _b.call(_a);
    },
    rowSelectedChanged(_states, row) {
      instance.store.toggleRowSelection(row);
      instance.store.updateAllSelected();
    },
    setHoverRow(states, row) {
      states.hoverRow.value = row;
    },
    setCurrentRow(_states, row) {
      instance.store.updateCurrentRow(row);
    }
  };
  const commit = function(name, ...args) {
    const mutations2 = instance.store.mutations;
    if (mutations2[name]) mutations2[name].apply(instance, [instance.store.states, ...args]);
    else throw new Error(`Action not found: ${name}`);
  };
  const updateTableScrollY = function() {
    Vue.nextTick(() => instance.layout.updateScrollY.apply(instance.layout));
  };
  return {
    ns,
    t,
    ...watcher$1,
    mutations,
    commit,
    updateTableScrollY
  };
}
exports.default = useStore;
