"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableRowContext = void 0;
exports.filterByInputValue = filterByInputValue;
exports.findColumnByFilterValue = findColumnByFilterValue;
exports.flatArray = flatArray;
exports.flatFilter = flatFilter;
exports.getColumnKey = getColumnKey;
exports.getHeight = getHeight;
exports.getLeafColumns = getLeafColumns;
exports.isNumber = isNumber;
exports.normalizeColumns = normalizeColumns;
exports.removeHiddenColumns = removeHiddenColumns;
exports.treeMap = treeMap;

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _react = require("react");

var _TableRowContext = _interopRequireDefault(require("../rc-components/table/TableRowContext"));

var TableRowContext = _TableRowContext["default"];
exports.TableRowContext = TableRowContext;

function flatArray() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var childrenName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'children';
  var result = [];

  var loop = function loop(array) {
    array.forEach(function (item) {
      if (item[childrenName]) {
        var newItem = (0, _objectSpread2["default"])({}, item);
        delete newItem[childrenName];
        result.push(newItem);

        if (item[childrenName].length > 0) {
          loop(item[childrenName]);
        }
      } else {
        result.push(item);
      }
    });
  };

  loop(data);
  return result;
}

function treeMap(tree, mapper) {
  var childrenName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'children';
  return tree.map(function (node, index) {
    var extra = {};

    if (node[childrenName]) {
      extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
    }

    return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, mapper(node, index)), extra);
  });
}

function flatFilter(tree, callback) {
  return tree.reduce(function (acc, node) {
    if (callback(node)) {
      acc.push(node);
    }

    if (node.children) {
      var children = flatFilter(node.children, callback);
      acc.push.apply(acc, (0, _toConsumableArray2["default"])(children));
    }

    return acc;
  }, []);
}

function normalizeColumns(elements) {
  var columns = [];

  _react.Children.forEach(elements, function (element) {
    if (! /*#__PURE__*/(0, _react.isValidElement)(element)) {
      return;
    }

    var column = (0, _objectSpread2["default"])({}, element.props);

    if (element.key) {
      column.key = element.key;
    }

    if (element.type && element.type.__C7N_TABLE_COLUMN_GROUP) {
      column.children = normalizeColumns(column.children);
    }

    columns.push(column);
  });

  return columns;
}

function getLeafColumns(columns) {
  return flatFilter(columns, function (c) {
    return !c.children;
  });
}

function getColumnKey(column, index) {
  return column.key || column.dataIndex || index;
}

function findColumnByFilterValue(record, columns, inputValue) {
  return columns.find(function (col) {
    var key = getColumnKey(col);

    if (key) {
      var value = record[key];

      if (value && (0, _typeof2["default"])(value) !== 'object') {
        value = value.toString();

        if (value.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1) {
          return true;
        }
      }
    }

    return false;
  });
}

function filterByInputValue(dataSource, columns, inputValue, cb) {
  dataSource.forEach(function (data) {
    var column = findColumnByFilterValue(data, columns, inputValue);

    if (column) {
      cb(data, column);
    }
  });
}

function removeHiddenColumns(columns) {
  return columns.filter(function (c) {
    if (c.hidden) {
      return false;
    }

    if (c.children) {
      var children = removeHiddenColumns(c.children);

      if (children.length) {
        c.children = children;
      } else {
        return false;
      }
    }

    return true;
  });
}

function getHeight(el) {
  return el.getBoundingClientRect().height;
}

function isNumber(obj) {
  return typeof obj === 'number' && !isNaN(obj);
}
//# sourceMappingURL=util.js.map
