import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";

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) { _defineProperty(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; }

import { createBEM } from '@zarm-design/bem';
import isEqual from 'lodash/isEqual';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import { useSafeState } from '../utils/hooks';
import Wheel from '../wheel';
import { isCascader, resolved } from './utils';
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(ConfigContext),
      prefixCls = _React$useContext.prefixCls;

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

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

  var _useSafeState = useSafeState(false),
      _useSafeState2 = _slicedToArray(_useSafeState, 2),
      stopScroll = _useSafeState2[0],
      setStopScroll = _useSafeState2[1];

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

  var _React$useMemo = React.useMemo(function () {
    return 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(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 (isCascader(props.dataSource)) {
      value.length = index + 1;
    }

    value[index] = selected;
    var next = 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, {
      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
};
export default PickerView;