"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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

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

var _popup = _interopRequireDefault(require("../../../popup"));

var _common = require("../../../common");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var prefixCls = (0, _common.getPrefixCls)('base-picker');
var heightUnit = 44;

var Picker = function Picker(props) {
  var _props$value = props.value,
      value = _props$value === void 0 ? 0 : _props$value,
      _props$range = props.range,
      range = _props$range === void 0 ? [] : _props$range,
      _props$rangeKey = props.rangeKey,
      rangeKey = _props$rangeKey === void 0 ? 'value' : _props$rangeKey,
      title = props.title,
      disabled = props.disabled,
      children = props.children,
      onChange = props.onChange;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var _useState3 = (0, _react.useState)(heightUnit * 2),
      _useState4 = _slicedToArray(_useState3, 2),
      initial = _useState4[0],
      setInitial = _useState4[1];

  var _useState5 = (0, _react.useState)(0),
      _useState6 = _slicedToArray(_useState5, 2),
      start = _useState6[0],
      setStart = _useState6[1];

  var _useState7 = (0, _react.useState)(initial),
      _useState8 = _slicedToArray(_useState7, 2),
      y = _useState8[0],
      setY = _useState8[1];

  var _useState9 = (0, _react.useState)(0),
      _useState10 = _slicedToArray(_useState9, 2),
      columnHeight = _useState10[0],
      setColumnHeight = _useState10[1];

  var _useState11 = (0, _react.useState)(false),
      _useState12 = _slicedToArray(_useState11, 2),
      isTransition = _useState12[0],
      setIsTransition = _useState12[1];

  (0, _react.useEffect)(function () {
    setColumnHeight(range.length * heightUnit);
  }, [range]);
  (0, _react.useEffect)(function () {
    if (!value || value <= 0) {
      setY(heightUnit * 2);
      return;
    }

    setY(-heightUnit * value + heightUnit * 2);
  }, [value]);

  var handleTap = function handleTap() {
    if (disabled) {
      return;
    }

    setOpen(function (state) {
      return !state;
    });
  };

  var handleCancel = function handleCancel() {
    setOpen(false);
  };

  var handleOK = function handleOK() {
    var v = Math.floor((y - heightUnit * 2) / -heightUnit);
    var e = {
      detail: {
        value: v
      }
    };
    onChange === null || onChange === void 0 ? void 0 : onChange(e);
    setOpen(false);
  };

  var handleTouchStart = function handleTouchStart(e) {
    var _a;

    (_a = e.preventDefault) === null || _a === void 0 ? void 0 : _a.call(e);
    setIsTransition(false);
    setStart(e.targetTouches[0].clientY);
  };

  var handleTouchMove = function handleTouchMove(e) {
    var _a;

    (_a = e.preventDefault) === null || _a === void 0 ? void 0 : _a.call(e);
    var diff = e.targetTouches[0].clientY - start;
    var updateY = diff + initial;

    if (updateY < -columnHeight + heightUnit * 2) {
      setY(-columnHeight + heightUnit * 2);
      return;
    }

    if (updateY > heightUnit * 3) {
      setY(heightUnit * 3);
      return;
    }

    setY(updateY);
  };

  var handleTouchEnd = function handleTouchEnd(e) {
    var _a;

    (_a = e.preventDefault) === null || _a === void 0 ? void 0 : _a.call(e);

    if (y < -columnHeight + heightUnit * 3) {
      setY(-columnHeight + heightUnit * 3);
      setInitial(-columnHeight + heightUnit * 3);
      setIsTransition(true);
      return;
    }

    if (y > heightUnit * 2) {
      setY(heightUnit * 2);
      setInitial(heightUnit * 2);
      setIsTransition(true);
      return;
    }

    if (y % heightUnit !== 0) {
      var r = Math.round(y / heightUnit) - Math.floor(y / heightUnit);

      if (r === 1) {
        var updateY = Math.ceil(y / heightUnit) * heightUnit;
        setY(updateY);
        setInitial(updateY);
        setIsTransition(true);
      } else {
        var _updateY = Math.floor(y / heightUnit) * heightUnit;

        setY(_updateY);
        setInitial(_updateY);
        setIsTransition(true);
      }

      return;
    }

    setInitial(y);
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: prefixCls
  }, /*#__PURE__*/_react.default.createElement("div", {
    onClick: handleTap
  }, children), /*#__PURE__*/_react.default.createElement(_popup.default, {
    position: "bottom",
    overlay: open,
    onClose: function onClose() {
      return setOpen(false);
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-container")
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-toolbar")
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-toolbar-cancel"),
    onClick: handleCancel
  }, "\u53D6\u6D88"), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-toolbar-tittle")
  }, title), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-toolbar-confirm"),
    onClick: handleOK
  }, "\u786E\u5B9A")), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-columns")
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-column")
  }, /*#__PURE__*/_react.default.createElement("ul", {
    className: "".concat(prefixCls, "-column-items"),
    onTouchStart: handleTouchStart,
    onTouchMove: handleTouchMove,
    onTouchEnd: handleTouchEnd,
    // onMouseDown={handleTouchStart}
    // onMouseMove={handleTouchMove}
    // onMouseUp={handleTouchEnd}
    style: {
      transform: "translate3d(0, ".concat(y, "PX, 0)"),
      transition: "".concat(isTransition ? 'transform' : 'none', " 0.2s cubic-bezier(0.645, 0.045, 0.355, 1)")
    }
  }, range === null || range === void 0 ? void 0 : range.map(function (option, index) {
    var _a;

    return /*#__PURE__*/_react.default.createElement("li", {
      key: index,
      className: "".concat(prefixCls, "-column-item")
    }, ((_a = option) === null || _a === void 0 ? void 0 : _a[rangeKey]) || option);
  }))), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-selected")
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: "".concat(prefixCls, "-mask")
  })))));
};

var _default = Picker;
exports.default = _default;