"use strict";

exports.__esModule = true;
exports.default = void 0;
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _style = require("../utils/style");
var _utils = require("./utils");
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var Picker = function (props, ref) {
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    data = props.data,
    className = props.className,
    valueProp = props.value,
    defaultValue = props.defaultValue,
    cascade = props.cascade,
    style = props.style,
    _props$onChange = props.onChange,
    onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
    _props$maskStyle = props.maskStyle,
    maskStyle = _props$maskStyle === void 0 ? {} : _props$maskStyle,
    _props$indicatorStyle = props.indicatorStyle,
    indicatorStyle = _props$indicatorStyle === void 0 ? {} : _props$indicatorStyle,
    others = (0, _tslib.__rest)(props, ["prefix", "data", "className", "value", "defaultValue", "cascade", "style", "onChange", "maskStyle", "indicatorStyle"]);
  var clsPrefix = prefix + "picker";
  var _useState = (0, _react.useState)(valueProp || defaultValue || []),
    innerValue = _useState[0],
    setInnerValue = _useState[1];
  var value = 'value' in props ? valueProp : innerValue;
  var dataArray = (0, _utils.getDataArray)(data, value, cascade);
  var curPickerIndex = (0, _utils.getIndex)(dataArray, value);
  var handleChange = function (e) {
    var newIndex = e.detail.value;
    var _dataArray = (0, _utils.getDataArrayByIndex)(data, newIndex, cascade);
    var ret = [];
    _dataArray.forEach(function (col, index) {
      var idx = newIndex[index] || 0;
      if (idx !== 0 && idx >= col.length) {
        idx = col.length - 1;
      }
      ret.push(col[idx].value);
    });
    onChange(ret, e);
    if (!('value' in props)) {
      setInnerValue(ret);
    }
  };
  (0, _react.useEffect)(function () {
    if (!('value' in props)) {
      return;
    }
    if (!valueProp) {
      if (innerValue.length !== 0 && defaultValue.length === 0) {
        setInnerValue([]);
        return;
      }
      if (innerValue.join(',') !== defaultValue.join(',')) {
        setInnerValue(defaultValue);
      }
      return;
    }
    if (innerValue.join(',') !== valueProp.join(',')) {
      setInnerValue(valueProp);
    }
  }, [innerValue, valueProp]);
  return /*#__PURE__*/(0, _react.createElement)("picker-view", _extends({
    className: (0, _classnames.default)(clsPrefix, className),
    value: curPickerIndex,
    onChange: handleChange,
    "mask-style": (0, _style.toStyleText)(maskStyle),
    "indicator-style": (0, _style.toStyleText)(Object.assign({
      height: 'var(--picker-height)'
    }, indicatorStyle)),
    ref: ref
  }, others), dataArray.map(function (colArr, colIndex) {
    return /*#__PURE__*/(0, _react.createElement)("picker-view-column", {
      key: colIndex
    }, colArr.map(function (item, itemIndex) {
      var _classNames;
      var isSelected = itemIndex === curPickerIndex[colIndex] || !curPickerIndex[colIndex] && itemIndex === 0;
      return /*#__PURE__*/(0, _react.createElement)("view", {
        key: itemIndex,
        className: (0, _classnames.default)(clsPrefix + "-col-item", (_classNames = {}, _classNames[clsPrefix + "-col-item--selected"] = isSelected, _classNames))
      }, item.label);
    }));
  }));
};
var _default = /*#__PURE__*/(0, _react.forwardRef)(Picker);
exports.default = _default;