var _excluded = ["visible", "value", "allowClear", "onChange", "list"];
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); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
import dimensionalArray from "./utils/dimensionalArray";
import formatValue from "./utils/formatValue";
import getIndex from "./utils/getIndex";
import Slots from "./Slots";
var Main = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _ref$visible = _ref.visible,
    visible = _ref$visible === void 0 ? true : _ref$visible,
    value = _ref.value,
    allowClear = _ref.allowClear,
    onChange = _ref.onChange,
    list = _ref.list,
    props = _objectWithoutProperties(_ref, _excluded);
  // 一维数组强制改成二维数组
  var lists = list;
  var listCount = 1;
  var dimensional = dimensionalArray(list);
  if (dimensional === 2) {
    listCount = list.length;
  } else if (dimensional === 1) {
    listCount = 1;
    lists = [list];
  } else {
    listCount = 0;
    lists = null;
  }

  // 节点
  var mainRef = useRef(null);
  var slotsRef = useRef(null);
  var valueRef = useRef(null);
  valueRef.current = formatValue(value, {
    lists: lists,
    listCount: listCount
  });
  useImperativeHandle(ref, function () {
    return {
      mainDOM: mainRef.current,
      getMainDOM: function getMainDOM() {
        return mainRef.current;
      },
      // 延迟解决Modal的useEffect visible 后执行的问题
      getValue: function getValue() {
        return valueRef.current;
      },
      update: update
    };
  });
  useEffect(function () {
    if (visible) {
      update();
    }
    // eslint-disable-next-line
  }, [visible, JSON.stringify(value)]);

  // 更新视图
  function update() {
    var _slotsRef$current, _slotsRef$current$que;
    var slots = (_slotsRef$current = slotsRef.current) === null || _slotsRef$current === void 0 || (_slotsRef$current$que = _slotsRef$current.querySelectorAll) === null || _slotsRef$current$que === void 0 ? void 0 : _slotsRef$current$que.call(_slotsRef$current, '.picker-slot');
    for (var i = 0; i < slots.length; i++) {
      var slot = slots[i];
      var y = -getIndex(valueRef.current[i], lists[i]) * 44;
      slot.style.transform = "translateY(".concat(y, "px)");
    }
  }
  return /*#__PURE__*/React.createElement("div", _extends({}, props, {
    className: "picker-main".concat(props.className ? ' ' + props.className : ''),
    ref: mainRef
  }), /*#__PURE__*/React.createElement("div", {
    className: "picker-layer"
  }, /*#__PURE__*/React.createElement("div", {
    className: "picker-layer-frame"
  })), /*#__PURE__*/React.createElement(Slots, {
    ref: slotsRef,
    lists: lists,
    onDragEnd: function onDragEnd(_ref2) {
      var rowIndex = _ref2.rowIndex,
        slotIndex = _ref2.slotIndex;
      valueRef.current[slotIndex] = lists[slotIndex][rowIndex];
      onChange && onChange(valueRef.current);
    }
  }));
});
export default Main;