"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _bem = require("@zarm-design/bem");

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var React = _interopRequireWildcard(require("react"));

var _configProvider = require("../config-provider");

var _hooks = require("../utils/hooks");

var _wheel = _interopRequireDefault(require("../wheel"));

var _utils = require("./utils");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var PickerView = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var className = props.className,
      style = props.style,
      cols = props.cols,
      dataSource = props.dataSource,
      fieldNames = props.fieldNames,
      itemRender = props.itemRender,
      disabled = props.disabled,
      onChange = props.onChange;

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls;

  var bem = (0, _bem.createBEM)('picker-view', {
    prefixCls: prefixCls
  });

  var _React$useState = React.useState((0, _utils.resolved)(props).value),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      innerValue = _React$useState2[0],
      setInnerValue = _React$useState2[1];

  var _useSafeState = (0, _hooks.useSafeState)(false),
      _useSafeState2 = (0, _slicedToArray2.default)(_useSafeState, 2),
      stopScroll = _useSafeState2[0],
      setStopScroll = _useSafeState2[1];

  React.useEffect(function () {
    if (props.value === undefined) return;
    if ((0, _isEqual.default)(props.value, innerValue)) return;
    setInnerValue((0, _utils.resolved)(props).value);
  }, [props.value]);

  var _React$useMemo = React.useMemo(function () {
    return (0, _utils.resolved)(_objectSpread(_objectSpread({}, props), {}, {
      value: innerValue
    }));
  }, [cols, innerValue, dataSource, fieldNames]),
      columns = _React$useMemo.columns,
      items = _React$useMemo.items;

  var reset = function reset() {
    setStopScroll(true);
    setTimeout(function () {
      setInnerValue((0, _utils.resolved)(props).value);
      setStopScroll(false);
    }, 0);
  };

  React.useImperativeHandle(ref, function () {
    return {
      value: innerValue,
      items: items,
      reset: reset
    };
  });
  var onValueChange = React.useCallback(function (selected, index) {
    var value = innerValue.slice();

    if ((0, _utils.isCascader)(props.dataSource)) {
      value.length = index + 1;
    }

    value[index] = selected;
    var next = (0, _utils.resolved)(_objectSpread(_objectSpread({}, props), {}, {
      value: value
    }));
    setInnerValue(next.value);
    onChange === null || onChange === void 0 ? void 0 : onChange(next.value, next.items, index);
  }, [innerValue]);
  return /*#__PURE__*/React.createElement("div", {
    className: bem([className]),
    style: style
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('content')
  }, columns.map(function (column, index) {
    return /*#__PURE__*/React.createElement(_wheel.default, {
      key: +index,
      dataSource: column,
      value: innerValue === null || innerValue === void 0 ? void 0 : innerValue[index],
      fieldNames: fieldNames,
      itemRender: itemRender ? function (item) {
        return itemRender(item, index);
      } : undefined,
      disabled: disabled,
      onChange: function onChange(selected) {
        return onValueChange(selected, index);
      },
      stopScroll: stopScroll
    });
  })), /*#__PURE__*/React.createElement("div", {
    className: bem('mask', [{
      top: true
    }])
  }), /*#__PURE__*/React.createElement("div", {
    className: bem('mask', [{
      bottom: true
    }])
  }));
});
PickerView.defaultProps = {
  defaultValue: [],
  dataSource: [],
  cols: Infinity,
  disabled: false
};
var _default = PickerView;
exports.default = _default;