"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const shared_esmBundler = require("../../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index = require("../../../../hooks/use-namespace/index.js");
const util = require("../util.js");
const tokens = require("../tokens.js");
const vue = require("vue");
function useStyles(props) {
  const parent = vue.inject(tokens.TABLE_INJECTION_KEY);
  const ns = index.useNamespace("table");
  const getRowStyle = (row, rowIndex) => {
    const rowStyle = parent == null ? void 0 : parent.props.rowStyle;
    if (shared_esmBundler.isFunction(rowStyle)) return rowStyle.call(null, {
      row,
      rowIndex
    });
    return rowStyle || null;
  };
  const getRowClass = (row, rowIndex, displayIndex) => {
    var _a;
    const classes = [ns.e("row")];
    if ((parent == null ? void 0 : parent.props.highlightCurrentRow) && row === ((_a = props.store) == null ? void 0 : _a.states.currentRow.value)) classes.push("current-row");
    if (props.stripe && displayIndex % 2 === 1) classes.push(ns.em("row", "striped"));
    const rowClassName = parent == null ? void 0 : parent.props.rowClassName;
    if (shared_esmBundler.isString(rowClassName)) classes.push(rowClassName);
    else if (shared_esmBundler.isFunction(rowClassName)) classes.push(rowClassName.call(null, {
      row,
      rowIndex
    }));
    return classes;
  };
  const getCellStyle = (rowIndex, columnIndex, row, column) => {
    const cellStyle = parent == null ? void 0 : parent.props.cellStyle;
    let cellStyles = cellStyle ?? {};
    if (shared_esmBundler.isFunction(cellStyle)) cellStyles = cellStyle.call(null, {
      rowIndex,
      columnIndex,
      row,
      column
    });
    const fixedStyle = util.getFixedColumnOffset(columnIndex, props == null ? void 0 : props.fixed, props.store);
    util.ensurePosition(fixedStyle, "left");
    util.ensurePosition(fixedStyle, "right");
    return Object.assign({}, cellStyles, fixedStyle);
  };
  const getCellClass = (rowIndex, columnIndex, row, column, offset) => {
    const fixedClasses = util.getFixedColumnsClass(ns.b(), columnIndex, props == null ? void 0 : props.fixed, props.store, void 0, offset);
    const classes = [
      column.id,
      column.align,
      column.className,
      ...fixedClasses
    ];
    const cellClassName = parent == null ? void 0 : parent.props.cellClassName;
    if (shared_esmBundler.isString(cellClassName)) classes.push(cellClassName);
    else if (shared_esmBundler.isFunction(cellClassName)) classes.push(cellClassName.call(null, {
      rowIndex,
      columnIndex,
      row,
      column
    }));
    classes.push(ns.e("cell"));
    return classes.filter((className) => Boolean(className)).join(" ");
  };
  const getSpan = (row, column, rowIndex, columnIndex) => {
    let rowspan = 1;
    let colspan = 1;
    const fn = parent == null ? void 0 : parent.props.spanMethod;
    if (shared_esmBundler.isFunction(fn)) {
      const result = fn({
        row,
        column,
        rowIndex,
        columnIndex
      });
      if (shared_esmBundler.isArray(result)) {
        rowspan = result[0];
        colspan = result[1];
      } else if (shared_esmBundler.isObject(result)) {
        rowspan = result.rowspan;
        colspan = result.colspan;
      }
    }
    return {
      rowspan,
      colspan
    };
  };
  const getColspanRealWidth = (columns, colspan, index2) => {
    if (colspan < 1) return columns[index2].realWidth;
    const widthArr = columns.map(({ realWidth, width }) => realWidth || width).slice(index2, index2 + colspan);
    return Number(widthArr.reduce((acc, width) => Number(acc) + Number(width), -1));
  };
  return {
    getRowStyle,
    getRowClass,
    getCellStyle,
    getCellClass,
    getSpan,
    getColspanRealWidth
  };
}
exports.default = useStyles;
