/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var isFunction = require('lodash/isFunction');
var get = require('lodash/get');
var isObject = require('lodash/isObject');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);

function toString(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}
function debounce(fn) {
  let delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  let timer;
  return function newFn() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    const context = this;
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(context, args);
    }, delay);
  };
}
function formatRowAttributes(attributes, params) {
  if (!attributes) return void 0;
  const attrList = Array.isArray(attributes) ? attributes : [attributes];
  let result = {};
  for (let i = 0; i < attrList.length; i++) {
    const attrItem = attrList[i];
    if (!attrItem) continue;
    const attrProperty = isFunction__default["default"](attrItem) ? attrItem(params) : attrItem;
    result = Array.isArray(attrProperty) ? formatRowAttributes(attrProperty, params) : Object.assign(result, attrProperty);
  }
  return result;
}
function formatRowClassNames(rowClassNames, params, rowKey) {
  const rowClassList = Array.isArray(rowClassNames) ? rowClassNames : [rowClassNames];
  const {
    row,
    rowIndex
  } = params;
  let customClasses = [];
  for (let i = 0, len = rowClassList.length; i < len; i++) {
    const rName = rowClassList[i];
    let tClass = isFunction__default["default"](rName) ? rName(params) : rName;
    if (isObject__default["default"](tClass) && !Array.isArray(tClass)) {
      tClass[rowIndex] && (tClass = tClass[rowIndex]);
      const rowId = get__default["default"](row, rowKey || "id");
      tClass[rowId] && (tClass = tClass[rowId]);
    } else if (Array.isArray(tClass)) {
      tClass = formatRowClassNames(tClass, params, rowKey);
    }
    customClasses = customClasses.concat(tClass);
  }
  return customClasses;
}
function formatClassNames(classNames, params) {
  const classes = Array.isArray(classNames) ? classNames : [classNames];
  const arr = [];
  for (let i = 0, len = classes.length; i < len; i++) {
    const cls = classes[i];
    if (isFunction__default["default"](cls)) arr.push(cls(params));else arr.push(cls);
  }
  return arr;
}
const INNER_PRE_NAME = "@@inner-";
var SCROLL_DIRECTION = /* @__PURE__ */(SCROLL_DIRECTION2 => {
  SCROLL_DIRECTION2["X"] = "x";
  SCROLL_DIRECTION2["Y"] = "y";
  SCROLL_DIRECTION2["UNKNOWN"] = "unknown";
  return SCROLL_DIRECTION2;
})(SCROLL_DIRECTION || {});
let preScrollLeft;
let preScrollTop;
const getScrollDirection = (scrollLeft, scrollTop) => {
  let direction = "unknown" /* UNKNOWN */;
  if (preScrollTop !== scrollTop) direction = "y" /* Y */;else if (preScrollLeft !== scrollLeft) direction = "x" /* X */;
  preScrollTop = scrollTop;
  preScrollLeft = scrollLeft;
  return direction;
};
const getRecord = record => {
  if (!record) return record;
  const result = {};
  Object.keys(record).forEach(key => {
    const descriptor = Object.getOwnPropertyDescriptor(record, key);
    descriptor && Reflect.defineProperty(result, key, {
      set(val) {
        descriptor.set(val);
      },
      get() {
        console.warn("The parameter `record` will be deprecated, please use `row` instead");
        return descriptor.get();
      }
    });
  });
  return result;
};
function isRowSelectedDisabled(selectColumn, row, rowIndex) {
  let disabled = isFunction__default["default"](selectColumn.disabled) ? selectColumn.disabled({
    row,
    rowIndex
  }) : selectColumn.disabled;
  if (selectColumn.checkProps) {
    if (isFunction__default["default"](selectColumn.checkProps)) disabled = disabled || selectColumn.checkProps({
      row,
      rowIndex
    }).disabled;else if (typeof selectColumn.checkProps === "object") disabled = disabled || selectColumn.checkProps.disabled;
  }
  return !!disabled;
}
function getCurrentRowByKey(columns, key) {
  if (!columns || !key) return;
  const col = columns?.find(t => t.colKey === key);
  if (col) return col;
  for (let i = 0, len = columns.length; i < len; i++) {
    if (columns[i]?.children?.length) return getCurrentRowByKey(columns[i]?.children, key);
  }
}
function getAffixProps(mainAffixProps, subAffixProps) {
  if (typeof mainAffixProps === "object") return mainAffixProps;
  if (typeof subAffixProps === "object") return subAffixProps;
  return {};
}
function getEditableKeysMap(keys, list, rowKey) {
  const map = {};
  for (let i = 0, len = list.length; i < len; i++) {
    const rowValue = get__default["default"](list[i], rowKey);
    if (keys.includes(rowValue)) map[rowValue] = true;
  }
  return map;
}
function getColumnDataByKey(columns, colKey) {
  for (let i = 0, len = columns.length; i < len; i++) {
    if (columns[i].colKey === colKey) return columns[i];
    if (columns[i].children?.length) {
      const t = getColumnDataByKey(columns[i].children, colKey);
      if (t) return t;
    }
  }
  return null;
}
function getColumnIndexByKey(columns, colKey) {
  for (let i = 0, len = columns.length; i < len; i++) {
    if (columns[i].colKey === colKey) return i;
    if (columns[i].children?.length) {
      const t = getColumnDataByKey(columns[i].children, colKey);
      if (t) return i;
    }
  }
  return -1;
}

exports.INNER_PRE_NAME = INNER_PRE_NAME;
exports.SCROLL_DIRECTION = SCROLL_DIRECTION;
exports.debounce = debounce;
exports.formatClassNames = formatClassNames;
exports.formatRowAttributes = formatRowAttributes;
exports.formatRowClassNames = formatRowClassNames;
exports.getAffixProps = getAffixProps;
exports.getColumnDataByKey = getColumnDataByKey;
exports.getColumnIndexByKey = getColumnIndexByKey;
exports.getCurrentRowByKey = getCurrentRowByKey;
exports.getEditableKeysMap = getEditableKeysMap;
exports.getRecord = getRecord;
exports.getScrollDirection = getScrollDirection;
exports.isRowSelectedDisabled = isRowSelectedDisabled;
exports.toString = toString;
//# sourceMappingURL=utils.js.map
