"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _tslib = require("tslib");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _meetReactComponentOne = require("@alifd/meet-react-component-one");
var _locale = require("../locale");
var _zhCn = _interopRequireDefault(require("../locale/lang/zh-cn"));
var _hooks = require("../utils/hooks");
var _view = _interopRequireDefault(require("../view"));
var _column = _interopRequireDefault(require("./column"));
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 formatValues = function (dataArray, value) {
  if (value === void 0) {
    value = [];
  }
  return dataArray.map(function (columnData, index) {
    if (columnData.length === 0) {
      console.error('[Picker] column\'s length cannot be 0, please check "data"', dataArray);
      return null;
    }
    return columnData.find(function (it) {
      return it.value === value[index];
    }) ? value[index] : columnData[0].value;
  });
};
var Picker = function (props, ref) {
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    _props$data = props.data,
    data = _props$data === void 0 ? [] : _props$data,
    cascade = props.cascade,
    _props$onChange = props.onChange,
    onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
    style = props.style,
    className = props.className,
    valueProp = props.value,
    defaultValue = props.defaultValue,
    others = (0, _tslib.__rest)(props, ["prefix", "data", "cascade", "onChange", "style", "className", "value", "defaultValue"]);
  var locale = (0, _locale.useLocale)('Picker', others, _zhCn.default.Picker);
  var _useValue = (0, _hooks.useValue)(props, [], {
      fitValue: function fitValue(x) {
        if (!Array.isArray(x)) {
          return [x];
        }
        return x;
      },
      compare: function compare(a, b) {
        if (b.length !== a.length) {
          return false;
        }
        return a.join(',') === b.join(',');
      }
    }),
    value = _useValue[0],
    setValue = _useValue[1],
    isControlled = _useValue[2];
  var handleChange = function (v, index) {
    var newValue = [].concat(value);
    newValue[index] = v;
    var formattedNewValue = formatValues((0, _utils.getDataArray)(data, newValue, cascade), newValue);
    onChange(formattedNewValue, index);
    if (!isControlled) {
      setValue(formattedNewValue);
    }
  };
  var dataArray = (0, _utils.getDataArray)(data, value, cascade);
  var values = formatValues(dataArray, value);
  return /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({}, others, {
    className: (0, _classnames.default)(prefix + "picker", prefix + "picker--web", className),
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style),
    ref: ref
  }), dataArray.map(function (d, index) {
    var columnValue = values[index] === undefined ? d[0].value : values[index];
    return /*#__PURE__*/(0, _react.createElement)(_column.default, {
      prefix: prefix,
      onChange: function (v) {
        return handleChange(v, index);
      },
      key: index,
      data: d,
      value: columnValue
    });
  }), dataArray && dataArray.length === 0 ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: prefix + "picker-text--empty"
  }, locale.empty) : null);
};
var _default = /*#__PURE__*/(0, _react.forwardRef)(Picker);
exports.default = _default;