function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useMemo, useState } from 'react';
import classNames from 'classnames';
import useInitLocalesOnce from "../../../../hooks/useInitLocalesOnce";
import CompensatedPisellContainer from "../../../../pro/compensatedPisellContainer";
import { formatDate, formatTime, toDate } from "../utils/time";
import localeTexts from "../locales";
import { t } from "../i18n";
var resolveOptionKey = function resolveOptionKey(option) {
  return String(option.key || option.snapshot.id || '');
};
export var SelectBookingsModal = function SelectBookingsModal(_ref) {
  var open = _ref.open,
    title = _ref.title,
    options = _ref.options,
    selectedKeysProp = _ref.selectedKeys,
    onCancel = _ref.onCancel,
    onConfirm = _ref.onConfirm;
  useInitLocalesOnce(localeTexts);
  var optionKeys = useMemo(function () {
    return options.map(resolveOptionKey);
  }, [options]);
  var initialSelectedKeys = selectedKeysProp !== null && selectedKeysProp !== void 0 && selectedKeysProp.length ? selectedKeysProp : optionKeys;
  var _useState = useState(initialSelectedKeys),
    _useState2 = _slicedToArray(_useState, 2),
    selectedKeys = _useState2[0],
    setSelectedKeys = _useState2[1];
  useEffect(function () {
    if (open) {
      setSelectedKeys(selectedKeysProp !== null && selectedKeysProp !== void 0 && selectedKeysProp.length ? selectedKeysProp : optionKeys);
    }
  }, [open, optionKeys, selectedKeysProp]);
  var selectedOptions = useMemo(function () {
    return options.filter(function (option) {
      return selectedKeys.includes(resolveOptionKey(option));
    });
  }, [options, selectedKeys]);
  var allSelected = selectedKeys.length === optionKeys.length && optionKeys.length > 0;
  var selectedCount = selectedOptions.length;
  var handleToggle = function handleToggle(key) {
    setSelectedKeys(function (prev) {
      return prev.includes(key) ? prev.filter(function (item) {
        return item !== key;
      }) : [].concat(_toConsumableArray(prev), [key]);
    });
  };
  return /*#__PURE__*/React.createElement(CompensatedPisellContainer, {
    renderType: "modal",
    props: {
      open: open,
      width: 920,
      title: t('pisell2.adjust-time.select-bookings-title'),
      onCancel: onCancel,
      className: 'pisell-adjust-time-select-bookings-modal',
      footer: /*#__PURE__*/React.createElement("footer", {
        className: "pisell-adjust-time-select-bookings__footer"
      }, /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-adjust-time-select-bookings__footer-btn",
        onClick: onCancel
      }, t('pisell2.adjust-time.cancel')), /*#__PURE__*/React.createElement("span", null), /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-adjust-time-select-bookings__footer-btn",
        onClick: function onClick() {
          return onConfirm(options);
        }
      }, t('pisell2.adjust-time.extend-all')), /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: "pisell-adjust-time-select-bookings__footer-btn pisell-adjust-time-select-bookings__footer-btn--primary",
        disabled: selectedCount === 0,
        onClick: function onClick() {
          return onConfirm(selectedOptions);
        }
      }, t('pisell2.adjust-time.extend-selected').replace('{count}', String(selectedCount))))
    }
  }, /*#__PURE__*/React.createElement("section", {
    className: "pisell-adjust-time-select-bookings"
  }, /*#__PURE__*/React.createElement("h3", null, t('pisell2.adjust-time.select-bookings-summary').replace('{count}', String(options.length)).replace('{title}', title)), /*#__PURE__*/React.createElement("p", null, t('pisell2.adjust-time.select-bookings-subtitle')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time-select-bookings__toolbar"
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: classNames('pisell-adjust-time-select-bookings__chip', {
      'pisell-adjust-time-select-bookings__chip--active': allSelected
    }),
    onClick: function onClick() {
      return setSelectedKeys(optionKeys);
    }
  }, t('pisell2.adjust-time.select-all')), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-adjust-time-select-bookings__chip",
    onClick: function onClick() {
      return setSelectedKeys([]);
    }
  }, t('pisell2.adjust-time.deselect-all'))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time-select-bookings__list"
  }, options.map(function (option) {
    var key = resolveOptionKey(option);
    var start = toDate(option.snapshot.start);
    var end = toDate(option.snapshot.end);
    var checked = selectedKeys.includes(key);
    var statusText = end <= new Date() ? t('pisell2.adjust-time.ended') : t('pisell2.adjust-time.active');
    return /*#__PURE__*/React.createElement("button", {
      key: key,
      type: "button",
      className: classNames('pisell-adjust-time-select-bookings__item', {
        'pisell-adjust-time-select-bookings__item--selected': checked
      }),
      onClick: function onClick() {
        return handleToggle(key);
      }
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-adjust-time-select-bookings__item-main"
    }, /*#__PURE__*/React.createElement("strong", null, "".concat(formatTime(start), " - ").concat(formatTime(end), " ").concat(formatDate(start))), /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("b", null, option.snapshot.id), /*#__PURE__*/React.createElement("em", null, option.customerName || t('pisell2.adjust-time.select-customer')))), /*#__PURE__*/React.createElement("span", {
      className: "pisell-adjust-time-select-bookings__status"
    }, statusText), /*#__PURE__*/React.createElement("span", {
      className: "pisell-adjust-time-select-bookings__checkbox"
    }, checked ? '✓' : ''));
  }))));
};
export default SelectBookingsModal;