"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Button = _interopRequireDefault(require("../Button"));

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

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

var defaultProps = {
  classPrefix: 'picker-toolbar',
  as: 'div'
};
/**
 * Toolbar for DatePicker and DateRangePicker
 */

var Toolbar = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      className = props.className,
      classPrefix = props.classPrefix,
      disabledOkBtn = props.disabledOkBtn,
      disabledShortcut = props.disabledShortcut,
      hideOkBtn = props.hideOkBtn,
      onOk = props.onOk,
      onClickShortcut = props.onClickShortcut,
      pageDate = props.pageDate,
      rangesProp = props.ranges,
      timeZone = props.timeZone,
      locale = props.locale,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "className", "classPrefix", "disabledOkBtn", "disabledShortcut", "hideOkBtn", "onOk", "onClickShortcut", "pageDate", "ranges", "timeZone", "locale"]);

  var _useState = (0, _react.useState)((0, _utils2.getRanges)(props)),
      ranges = _useState[0],
      setRanges = _useState[1];

  var _useClassNames = (0, _utils.useClassNames)(classPrefix),
      merge = _useClassNames.merge,
      prefix = _useClassNames.prefix,
      withClassPrefix = _useClassNames.withClassPrefix;

  (0, _react.useEffect)(function () {
    setRanges((0, _utils2.getRanges)({
      ranges: rangesProp,
      pageDate: pageDate,
      timeZone: timeZone
    }));
  }, [pageDate, rangesProp, timeZone]);
  var hasLocaleKey = (0, _react.useCallback)(function (key) {
    return (0, _utils2.getDefaultRanges)(timeZone, pageDate).some(function (item) {
      return item.label === key;
    });
  }, [pageDate, timeZone]);
  var renderOkButton = (0, _react.useCallback)(function () {
    if (hideOkBtn) {
      return null;
    }

    var disabled = disabledOkBtn === null || disabledOkBtn === void 0 ? void 0 : disabledOkBtn(pageDate);
    return /*#__PURE__*/_react.default.createElement("div", {
      className: prefix('right')
    }, /*#__PURE__*/_react.default.createElement(_Button.default, {
      appearance: "primary",
      size: "sm",
      disabled: disabled,
      onClick: disabled ? undefined : onOk
    }, locale === null || locale === void 0 ? void 0 : locale.ok));
  }, [disabledOkBtn, hideOkBtn, locale, onOk, pageDate, prefix]);

  if (hideOkBtn && ranges.length === 0) {
    return null;
  }

  var classes = merge(className, withClassPrefix());
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: prefix('ranges')
  }, ranges.map(function (_ref, index) {
    var value = _ref.value,
        closeOverlay = _ref.closeOverlay,
        label = _ref.label;
    var disabled = disabledShortcut === null || disabledShortcut === void 0 ? void 0 : disabledShortcut(value);

    var handleClickShortcut = function handleClickShortcut(event) {
      if (disabled) {
        return;
      }

      onClickShortcut === null || onClickShortcut === void 0 ? void 0 : onClickShortcut(value, closeOverlay, event);
    };

    return /*#__PURE__*/_react.default.createElement(_Button.default, {
      appearance: "link",
      size: "sm",
      key: index,
      disabled: disabled,
      onClick: handleClickShortcut
    }, hasLocaleKey(label) && typeof label === 'string' ? locale === null || locale === void 0 ? void 0 : locale[label] : label);
  })), renderOkButton());
});

Toolbar.displayName = 'Toolbar';
Toolbar.propTypes = {
  ranges: _propTypes.default.array,
  className: _propTypes.default.string,
  classPrefix: _propTypes.default.string,
  pageDate: _propTypes.default.oneOfType([_propTypes.default.instanceOf(Date), _propTypes.default.arrayOf(_propTypes.default.instanceOf(Date))]),
  onClickShortcut: _propTypes.default.func,
  onOk: _propTypes.default.func,
  disabledShortcut: _propTypes.default.func,
  disabledOkBtn: _propTypes.default.func,
  hideOkButton: _propTypes.default.bool,
  timeZone: _propTypes.default.string
};
Toolbar.defaultProps = defaultProps;
var _default = Toolbar;
exports.default = _default;