"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const tokens = require("../tokens.js");
const Vue = require("vue");
const getAllColumns = (columns) => {
  const result = [];
  columns.forEach((column) => {
    if (column.children) {
      result.push(column);
      result.push.apply(result, getAllColumns(column.children));
    } else result.push(column);
  });
  return result;
};
const convertToRows = (originColumns) => {
  let maxLevel = 1;
  const traverse = (column, parent) => {
    if (parent) {
      column.level = parent.level + 1;
      if (maxLevel < column.level) maxLevel = column.level;
    }
    if (column.children) {
      let colSpan = 0;
      column.children.forEach((subColumn) => {
        traverse(subColumn, column);
        colSpan += subColumn.colSpan;
      });
      column.colSpan = colSpan;
    } else column.colSpan = 1;
  };
  originColumns.forEach((column) => {
    column.level = 1;
    traverse(column, void 0);
  });
  const rows = [];
  for (let i = 0; i < maxLevel; i++) rows.push([]);
  getAllColumns(originColumns).forEach((column) => {
    if (!column.children) column.rowSpan = maxLevel - column.level + 1;
    else {
      column.rowSpan = 1;
      column.children.forEach((col) => col.isSubColumn = true);
    }
    rows[column.level - 1].push(column);
  });
  return rows;
};
function useUtils(props) {
  const parent = Vue.inject(tokens.TABLE_INJECTION_KEY);
  const columnRows = Vue.computed(() => {
    return convertToRows(props.store.states.originColumns.value);
  });
  const isGroup = Vue.computed(() => {
    const result = columnRows.value.length > 1;
    if (result && parent) parent.state.isGroup.value = true;
    return result;
  });
  const toggleAllSelection = (event) => {
    event.stopPropagation();
    parent == null ? void 0 : parent.store.commit("toggleAllSelection");
  };
  return {
    isGroup,
    toggleAllSelection,
    columnRows
  };
}
exports.convertToRows = convertToRows;
exports.default = useUtils;
