import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import React, { useCallback, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Button from '../Button';
import { useClassNames } from '../utils';
import { getDefaultRanges, getRanges } from './utils';
var defaultProps = {
  classPrefix: 'picker-toolbar',
  as: 'div'
};
/**
 * Toolbar for DatePicker and DateRangePicker
 */

var Toolbar = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "className", "classPrefix", "disabledOkBtn", "disabledShortcut", "hideOkBtn", "onOk", "onClickShortcut", "pageDate", "ranges", "timeZone", "locale"]);

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

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

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

    var disabled = disabledOkBtn === null || disabledOkBtn === void 0 ? void 0 : disabledOkBtn(pageDate);
    return /*#__PURE__*/React.createElement("div", {
      className: prefix('right')
    }, /*#__PURE__*/React.createElement(Button, {
      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.createElement(Component, _extends({}, rest, {
    ref: ref,
    className: classes
  }), /*#__PURE__*/React.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.createElement(Button, {
      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.array,
  className: PropTypes.string,
  classPrefix: PropTypes.string,
  pageDate: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.arrayOf(PropTypes.instanceOf(Date))]),
  onClickShortcut: PropTypes.func,
  onOk: PropTypes.func,
  disabledShortcut: PropTypes.func,
  disabledOkBtn: PropTypes.func,
  hideOkButton: PropTypes.bool,
  timeZone: PropTypes.string
};
Toolbar.defaultProps = defaultProps;
export default Toolbar;