"use strict";

exports.__esModule = true;
exports.getDataSource = getDataSource;
exports.getDataSourceFromChildren = getDataSourceFromChildren;
exports.makeChain = makeChain;
exports.mergeDataSource = mergeDataSource;
exports.px2rpx = px2rpx;
exports.stopPropagation = stopPropagation;
var _tslib = require("tslib");
var _env = require("./env");
var _react = _interopRequireWildcard(require("react"));
var _fp = require("./fp");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function makeChain() {
  for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {
    fns[_key] = arguments[_key];
  }
  if (fns.length === 1) {
    return fns[0];
  }
  return function () {
    var i = 0;
    var j = fns.length;
    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }
    for (; i < j; i++) {
      if (fns[i] && fns[i].apply) {
        fns[i].apply(this, args);
      }
    }
  };
}
function px2rpx(val) {
  return val / _env.screenRate;
}
function stopPropagation(e) {
  if (!e) {
    return;
  }
  if (e.stopPropagation) {
    e.stopPropagation();
    return;
  }
  if (typeof e.originalEvent === 'object' && e.originalEvent.stopPropagation) {
    e.originalEvent.stopPropagation();
  }
}
function getDataSourceFromChildren(children) {
  if (!children) {
    return [];
  }
  return _react.Children.map(children, function (child) {
    var _a = child.props,
      value = _a.value,
      text = _a.text,
      childText = _a.children,
      rest = (0, _tslib.__rest)(_a, ["value", "text", "children"]);
    return Object.assign({
      value: value,
      label: childText || text
    }, rest);
  });
}
function getDataSource(dataSource, children) {
  var ds = dataSource;
  if (!dataSource && _react.Children.count(children) > 0) {
    ds = getDataSourceFromChildren(children);
  }
  return ds || [];
}
function mergeDataSource(ds1, ds2) {
  var tmpArr = [];
  ds2.forEach(function (it) {
    if ((it === null || it === void 0 ? void 0 : it.value) && !(0, _fp.findInArray)([].concat(ds1, tmpArr), function (_it) {
      return _it.value === it.value;
    })) {
      tmpArr.push(it);
    }
  });
  return [].concat(tmpArr, ds1);
}