"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const constants = require("../constants.js");
const _private = require("../private.js");
const utils = require("./utils.js");
const Vue = require("vue");
function useColumns(props, columns, fixed) {
  const _columns = Vue.computed(() => Vue.unref(columns).map((column, index) => ({
    ...column,
    key: column.key ?? column.dataKey ?? index
  })));
  const visibleColumns = Vue.computed(() => {
    return Vue.unref(_columns).filter((column) => !column.hidden);
  });
  const fixedColumnsOnLeft = Vue.computed(() => Vue.unref(visibleColumns).filter((column) => column.fixed === "left" || column.fixed === true));
  const fixedColumnsOnRight = Vue.computed(() => Vue.unref(visibleColumns).filter((column) => column.fixed === "right"));
  const normalColumns = Vue.computed(() => Vue.unref(visibleColumns).filter((column) => !column.fixed));
  const mainColumns = Vue.computed(() => {
    const ret = [];
    Vue.unref(fixedColumnsOnLeft).forEach((column) => {
      ret.push({
        ...column,
        placeholderSign: _private.placeholderSign
      });
    });
    Vue.unref(normalColumns).forEach((column) => {
      ret.push(column);
    });
    Vue.unref(fixedColumnsOnRight).forEach((column) => {
      ret.push({
        ...column,
        placeholderSign: _private.placeholderSign
      });
    });
    return ret;
  });
  const hasFixedColumns = Vue.computed(() => {
    return Vue.unref(fixedColumnsOnLeft).length || Vue.unref(fixedColumnsOnRight).length;
  });
  const columnsStyles = Vue.computed(() => {
    return Vue.unref(_columns).reduce((style, column) => {
      style[column.key] = utils.calcColumnStyle(column, Vue.unref(fixed), props.fixed);
      return style;
    }, {});
  });
  const columnsTotalWidth = Vue.computed(() => {
    return Vue.unref(visibleColumns).reduce((width, column) => width + column.width, 0);
  });
  const getColumn = (key) => {
    return Vue.unref(_columns).find((column) => column.key === key);
  };
  const getColumnStyle = (key) => {
    return Vue.unref(columnsStyles)[key];
  };
  const updateColumnWidth = (column, width) => {
    column.width = width;
  };
  function onColumnSorted(e) {
    var _a;
    const { key } = e.currentTarget.dataset;
    if (!key) return;
    const { sortState, sortBy } = props;
    let order = constants.SortOrder.ASC;
    if (shared_esmBundler.isObject(sortState)) order = constants.oppositeOrderMap[sortState[key]];
    else order = constants.oppositeOrderMap[sortBy.order];
    (_a = props.onColumnSort) == null ? void 0 : _a.call(props, {
      column: getColumn(key),
      key,
      order
    });
  }
  return {
    columns: _columns,
    columnsStyles,
    columnsTotalWidth,
    fixedColumnsOnLeft,
    fixedColumnsOnRight,
    hasFixedColumns,
    mainColumns,
    normalColumns,
    visibleColumns,
    getColumn,
    getColumnStyle,
    updateColumnWidth,
    onColumnSorted
  };
}
exports.useColumns = useColumns;
