/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var isFunction = require('lodash/isFunction');
var get = require('lodash/get');

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

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

function toString(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}
function debounce(fn) {
  var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  var timer;
  return function newFn() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    var context = this;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
}
function formatRowAttributes(attributes, params) {
  if (!attributes) return void 0;
  var attrList = attributes instanceof Array ? attributes : [attributes];
  var result = {};

  for (var i = 0; i < attrList.length; i++) {
    var attrItem = attrList[i];
    if (!attrItem) continue;
    var attrProperty = isFunction__default["default"](attrItem) ? attrItem(params) : attrItem;
    result = attrProperty instanceof Array ? formatRowAttributes(attrProperty, params) : Object.assign(result, attrProperty);
  }

  return result;
}
function formatRowClassNames(rowClassNames, params, rowKey) {
  var rowClassList = rowClassNames instanceof Array ? rowClassNames : [rowClassNames];
  var row = params.row,
      rowIndex = params.rowIndex;
  var customClasses = [];

  for (var i = 0, len = rowClassList.length; i < len; i++) {
    var rName = rowClassList[i];
    var tClass = isFunction__default["default"](rName) ? rName(params) : rName;

    if (_typeof__default["default"](tClass) === "object") {
      tClass[rowIndex] && (tClass = tClass[rowIndex]);
      var rowId = get__default["default"](row, rowKey || "id");
      tClass[rowId] && (tClass = tClass[rowId]);
    }

    customClasses = customClasses.concat(tClass);
  }

  return customClasses;
}
function filterDataByIds() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var ids = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var byId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "id";
  return data.filter(function () {
    var d = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return ids.includes(d[byId]);
  });
}
var INNER_PRE_NAME = "@@inner-";
var SCROLL_DIRECTION = /* @__PURE__ */function (SCROLL_DIRECTION2) {
  SCROLL_DIRECTION2["X"] = "x";
  SCROLL_DIRECTION2["Y"] = "y";
  SCROLL_DIRECTION2["UNKNOWN"] = "unknown";
  return SCROLL_DIRECTION2;
}(SCROLL_DIRECTION || {});
var preScrollLeft;
var preScrollTop;
var getScrollDirection = function getScrollDirection(scrollLeft, scrollTop) {
  var direction = "unknown"
  /* UNKNOWN */
  ;

  if (preScrollTop !== scrollTop) {
    direction = "y"
    /* Y */
    ;
  } else if (preScrollLeft !== scrollLeft) {
    direction = "x"
    /* X */
    ;
  }

  preScrollTop = scrollTop;
  preScrollLeft = scrollLeft;
  return direction;
};
var getRecord = function getRecord(record) {
  if (!record) {
    return record;
  }

  var result = {};
  Object.keys(record).forEach(function (key) {
    var descriptor = Object.getOwnPropertyDescriptor(record, key);
    descriptor && Reflect.defineProperty(result, key, {
      set: function set(val) {
        descriptor.set(val);
      },
      get: function get() {
        console.warn("The parameter `record` will be deprecated, please use `row` instead");
        return descriptor.get();
      }
    });
  });
  return result;
};
function isRowSelectedDisabled(selectColumn, row, rowIndex) {
  var disabled = isFunction__default["default"](selectColumn.disabled) ? selectColumn.disabled({
    row: row,
    rowIndex: rowIndex
  }) : selectColumn.disabled;

  if (selectColumn.checkProps) {
    if (isFunction__default["default"](selectColumn.checkProps)) {
      disabled = disabled || selectColumn.checkProps({
        row: row,
        rowIndex: rowIndex
      }).disabled;
    } else if (selectColumn.checkProps === "object") {
      disabled = disabled || selectColumn.checkProps.disabled;
    }
  }

  return !!disabled;
}

exports.INNER_PRE_NAME = INNER_PRE_NAME;
exports.SCROLL_DIRECTION = SCROLL_DIRECTION;
exports.debounce = debounce;
exports.filterDataByIds = filterDataByIds;
exports.formatRowAttributes = formatRowAttributes;
exports.formatRowClassNames = formatRowClassNames;
exports.getRecord = getRecord;
exports.getScrollDirection = getScrollDirection;
exports.isRowSelectedDisabled = isRowSelectedDisabled;
exports.toString = toString;
//# sourceMappingURL=utils.js.map
