"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PickerView = void 0;
var _react = _interopRequireWildcard(require("react"));
var _withDefaultProps = require("../../utils/with-default-props");
var _wheel = require("./wheel");
var _columnsExtend = require("./columns-extend");
var _nativeProps = require("../../utils/native-props");
var _ahooks = require("ahooks");
var _pickerUtils = require("../picker/picker-utils");
var _spinLoading = _interopRequireDefault(require("../spin-loading"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const classPrefix = `adm-picker-view`;
const defaultProps = {
  defaultValue: [],
  renderLabel: _pickerUtils.defaultRenderLabel,
  mouseWheel: false,
  loadingContent: _react.default.createElement("div", {
    className: `${classPrefix}-loading-content`
  }, _react.default.createElement(_spinLoading.default, null))
};
const PickerView = exports.PickerView = (0, _react.memo)(p => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  const [innerValue, setInnerValue] = (0, _react.useState)(props.value === undefined ? props.defaultValue : props.value);
  // Sync `value` to `innerValue`
  (0, _react.useEffect)(() => {
    if (props.value === undefined) return; // Uncontrolled mode
    if (props.value === innerValue) return;
    setInnerValue(props.value);
  }, [props.value]);
  (0, _react.useEffect)(() => {
    if (props.value === innerValue) return;
    const timeout = window.setTimeout(() => {
      if (props.value !== undefined && props.value !== innerValue) {
        setInnerValue(props.value);
      }
    }, 1000);
    return () => {
      window.clearTimeout(timeout);
    };
  }, [props.value, innerValue]);
  const extend = (0, _columnsExtend.useColumnsExtend)(props.columns, innerValue);
  const columns = extend.columns;
  (0, _ahooks.useDebounceEffect)(() => {
    var _a;
    if (props.value === innerValue) return;
    (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, innerValue, extend);
  }, [innerValue], {
    wait: 0,
    leading: false,
    trailing: true
  });
  const handleSelect = (0, _react.useCallback)((val, index) => {
    setInnerValue(prev => {
      const next = [...prev];
      next[index] = val;
      return next;
    });
  }, []);
  return (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    className: `${classPrefix}`
  }, props.loading ? props.loadingContent : _react.default.createElement(_react.default.Fragment, null, columns.map((column, index) => _react.default.createElement(_wheel.Wheel, {
    key: index,
    index: index,
    column: column,
    value: innerValue[index],
    onSelect: handleSelect,
    renderLabel: props.renderLabel,
    mouseWheel: props.mouseWheel
  })), _react.default.createElement("div", {
    className: `${classPrefix}-mask`
  }, _react.default.createElement("div", {
    className: `${classPrefix}-mask-top`
  }), _react.default.createElement("div", {
    className: `${classPrefix}-mask-middle`
  }), _react.default.createElement("div", {
    className: `${classPrefix}-mask-bottom`
  })))));
});
PickerView.displayName = 'PickerView';