"use strict";

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

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

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

var _pickerPanels = _interopRequireDefault(require("./picker-panels"));

var _pickerTrigger = _interopRequireDefault(require("./components/picker-trigger"));

var _context = _interopRequireDefault(require("./context"));

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

require("./index.css");

var _excluded = ["children", "direction", "displayMode", "format", "max", "min", "multiple", "picker", "popupPlacement", "settingType", "value", "visible", "onChange", "onPopupVisibleChange"];

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 _extends() { _extends = Object.assign || 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 _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; }

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; }

var Picker = function Picker(props) {
  var children = props.children,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'ltr' : _props$direction,
      _props$displayMode = props.displayMode,
      displayMode = _props$displayMode === void 0 ? 'layer' : _props$displayMode,
      _props$format = props.format,
      format = _props$format === void 0 ? _utils.PickerFormat : _props$format,
      _props$max = props.max,
      max = _props$max === void 0 ? _utils.PickerMaxDate : _props$max,
      _props$min = props.min,
      min = _props$min === void 0 ? _utils.PickerMinDate : _props$min,
      _props$multiple = props.multiple,
      multiple = _props$multiple === void 0 ? false : _props$multiple,
      _props$picker = props.picker,
      picker = _props$picker === void 0 ? 'date' : _props$picker,
      _props$popupPlacement = props.popupPlacement,
      popupPlacement = _props$popupPlacement === void 0 ? 'bottomLeft' : _props$popupPlacement,
      _props$settingType = props.settingType,
      settingType = _props$settingType === void 0 ? 'startDate' : _props$settingType,
      _props$value = props.value,
      value = _props$value === void 0 ? [(0, _moment["default"])()] : _props$value,
      _props$visible = props.visible,
      visible = _props$visible === void 0 ? false : _props$visible,
      onChange = props.onChange,
      onPopupVisibleChange = props.onPopupVisibleChange,
      restProps = _objectWithoutProperties(props, _excluded);

  var _useState = (0, _react.useState)(visible),
      _useState2 = _slicedToArray(_useState, 2),
      popupVisible = _useState2[0],
      setPopupVisible = _useState2[1];

  var getPopupPlacement = function getPopupPlacement() {
    if (popupPlacement !== undefined) {
      return popupPlacement;
    }

    return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
  };

  var onCloseHandler = function onCloseHandler() {
    return setPopupVisible(false);
  };

  var onChangeHandler = function onChangeHandler(date) {
    var newDate = date.map(function (item) {
      return item ? (0, _moment["default"])(item).format(format) : item;
    });
    var result = onChange(newDate);

    if (result && result instanceof Promise) {
      result.then(function (res) {
        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        res && onCloseHandler();
      });
    } else if (result !== false) {
      onCloseHandler();
    }
  };

  var onPopupVisibleChangeHandler = function onPopupVisibleChangeHandler(visible) {
    setPopupVisible(visible);
    onPopupVisibleChange === null || onPopupVisibleChange === void 0 ? void 0 : onPopupVisibleChange(visible);
  };

  var popContent = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement(_pickerPanels["default"], _extends({
      onChange: onChangeHandler,
      onClose: onCloseHandler
    }, restProps));
  }, [value]);
  return /*#__PURE__*/_react["default"].createElement(_context["default"].Provider, {
    value: {
      picker: picker,
      format: format,
      max: max,
      min: min,
      multiple: multiple,
      settingType: settingType,
      value: value
    }
  }, displayMode === 'layer' && /*#__PURE__*/_react["default"].createElement(_popup["default"], _extends({
    destroyPopupOnHide: true,
    extraClass: (0, _classnames["default"])(_utils.PickerDropdownClass),
    popup: popContent,
    popupVisible: popupVisible,
    popupPlacement: getPopupPlacement(),
    onPopupVisibleChange: onPopupVisibleChangeHandler
  }, restProps), /*#__PURE__*/_react["default"].createElement(_pickerTrigger["default"], _extends({
    focused: popupVisible,
    viewDate: (0, _moment["default"])(value[0]),
    onClick: function onClick() {
      return setPopupVisible(true);
    }
  }, restProps), children)), displayMode === 'tiled' && popContent);
};

var _default = Picker;
exports["default"] = _default;