"use strict";

exports.__esModule = true;
exports.defaultLocalFilter = defaultLocalFilter;
exports.getInitValues = getInitValues;
exports.getPreviewValue = getPreviewValue;
exports.getScrollHeight = getScrollHeight;
exports.getValidEvtValues = getValidEvtValues;
exports.innerRenderSelection = innerRenderSelection;
exports.isEqualItems = isEqualItems;
exports.isSelected = isSelected;
exports.removeItemFromArray = removeItemFromArray;
var _react = _interopRequireWildcard(require("react"));
var _env = require("@uni/env");
var _utils = require("../utils");
var _selectValue = _interopRequireDefault(require("./section/select-value"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 removeItemFromArray(arr, val) {
  if (arr === void 0) {
    arr = [];
  }
  if (val === void 0) {
    val = '';
  }
  return arr.filter(function (v) {
    return v.value !== val;
  });
}
function getInitValues(props) {
  var defaultValue = props.defaultValue,
    value = props.value;
  var initValues;
  if ('value' in props) {
    initValues = value;
  } else if (!(0, _utils.isUndef)(defaultValue)) {
    initValues = defaultValue;
  }
  if (!Array.isArray(initValues)) {
    if (!(0, _utils.isUndef)(initValues)) {
      initValues = [initValues];
    } else {
      initValues = [];
    }
  }
  return initValues;
}
function getPreviewValue(props) {
  var useDetailValue = props.useDetailValue,
    children = props.children,
    dataSource = props.dataSource,
    mode = props.mode;
  var initValues = getInitValues(props);
  var ds = dataSource || [];
  var arr = [];
  if (!dataSource && _react.Children.count(children) > 0) {
    ds = (0, _utils.getDataSourceFromChildren)(children);
  }
  if (useDetailValue && (0, _utils.isValidArray)(initValues)) {
    arr = initValues;
  } else if ((0, _utils.isValidArray)(initValues)) {
    arr = initValues.map(function (v) {
      var rs = ds.find(function (it) {
        return it.value === v;
      });
      return rs || {
        label: v,
        value: v
      };
    });
  }
  if (mode === 'single') {
    return (0, _utils.isValidArray)(arr) ? arr[0] : '';
  }
  return arr;
}
function getScrollHeight() {
  if (_env.isWeb) {
    return (screen.availHeight - 200) / (screen.width / 750) + "rpx";
  } else if (_utils.isMiniappPlatform) {
    return _utils.windowHeight - 100;
  }
  return 0;
}
function innerRenderSelection(items, values) {
  var labels = !Array.isArray(items) && items.label ? [items.label] : items.map(function (item) {
    return item.label;
  });
  if (labels.length === 0) {
    return null;
  }
  return /*#__PURE__*/(0, _react.createElement)(_selectValue.default, Object.assign(Object.assign({}, values), {
    items: labels
  }));
}
function isSelected(curItems, value) {
  var rs = (0, _utils.findInArray)(curItems, function (item) {
    return item.value === value;
  });
  return !!rs;
}
function isEqualItems(arr1, arr2, strictMode) {
  if (arr1 === void 0) {
    arr1 = [];
  }
  if (arr2 === void 0) {
    arr2 = [];
  }
  if (strictMode === void 0) {
    strictMode = false;
  }
  if (!Array.isArray(arr1) || !Array.isArray(arr2) || arr1.length !== arr2.length) {
    return false;
  }
  var filterFn = function (item) {
    return item.value;
  };
  if (strictMode) {
    filterFn = function (item) {
      return item.label + item.value;
    };
  }
  return arr1.map(filterFn).sort().toString() === arr2.map(filterFn).sort().toString();
}
function getValidEvtValues(_ref) {
  var selectedItems = _ref.selectedItems,
    useDetailValue = _ref.useDetailValue,
    isSingle = _ref.isSingle;
  var value = isSingle ? null : [];
  var items = isSingle ? {} : [];
  if ((0, _utils.isValidArray)(selectedItems)) {
    value = selectedItems[0].value;
    if (useDetailValue) {
      value = selectedItems;
    } else if (!isSingle) {
      value = selectedItems.map(function (item) {
        return item.value;
      });
    }
    items = isSingle ? selectedItems[0] : selectedItems;
  }
  return {
    value: value,
    items: items
  };
}
function defaultLocalFilter(searchText, item) {
  try {
    return new RegExp(searchText).test(item.label);
  } catch (e) {
    return item.label.includes(searchText);
  }
}