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, { useMemo } from 'react';
import { locales } from '@pisell/utils';
import "./ShiftPreview.less";
var WEEKDAYS = [1, 2, 3, 4, 5, 6, 7];
var getWeekdayText = function getWeekdayText(weekday) {
  return locales.getText("pisell2.attendance.group-settings.shift-schedule.modal.weekday.".concat(weekday));
};
var parseMinutes = function parseMinutes(hhmm) {
  var _split$map = (hhmm || '').split(':').map(function (v) {
      return Number(v);
    }),
    _split$map2 = _slicedToArray(_split$map, 2),
    h = _split$map2[0],
    m = _split$map2[1];
  if (!Number.isFinite(h) || !Number.isFinite(m)) {
    return null;
  }
  return h * 60 + m;
};
var formatClock = function formatClock(hhmm) {
  var totalMinutes = parseMinutes(hhmm);
  if (totalMinutes === null) return '';
  var hours24 = Math.floor(totalMinutes / 60) % 24;
  var minutes = totalMinutes % 60;
  var suffix = hours24 >= 12 ? locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.period.pm') : locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.period.am');
  var hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12;
  return "".concat(hours12, ":").concat(String(minutes).padStart(2, '0'), " ").concat(suffix);
};
var formatWeeklyHours = function formatWeeklyHours(totalMinutes) {
  var hours = totalMinutes / 60;
  if (Number.isInteger(hours)) return "".concat(hours);
  return "".concat(hours.toFixed(2)).replace(/\.?0+$/, '');
};
var formatDuration = function formatDuration(totalMinutes) {
  var hours = Math.floor(totalMinutes / 60);
  var minutes = totalMinutes % 60;
  return locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.duration')(hours, minutes);
};
var ShiftPreview = function ShiftPreview(props) {
  var schedule = Array.isArray(props.value) ? props.value : [];
  var previewDays = useMemo(function () {
    var byWeekday = new Map();
    schedule.forEach(function (item) {
      if (item && typeof item.weekday === 'number') {
        byWeekday.set(item.weekday, item);
      }
    });
    return WEEKDAYS.map(function (weekday) {
      var item = byWeekday.get(weekday);
      var times = Array.isArray(item === null || item === void 0 ? void 0 : item.times) ? item.times : [];
      var timeRanges = times.map(function (time) {
        var startText = formatClock(time === null || time === void 0 ? void 0 : time.start);
        var endText = formatClock(time === null || time === void 0 ? void 0 : time.end);
        if (!startText || !endText) return null;
        return {
          startText: startText,
          endText: endText
        };
      }).filter(Boolean);
      var totalMinutes = times.reduce(function (sum, time) {
        var start = parseMinutes(time === null || time === void 0 ? void 0 : time.start);
        var end = parseMinutes(time === null || time === void 0 ? void 0 : time.end);
        if (start === null || end === null || end <= start) {
          return sum;
        }
        return sum + (end - start);
      }, 0);
      return {
        weekday: weekday,
        timeRanges: timeRanges,
        totalMinutes: totalMinutes
      };
    });
  }, [schedule]);
  var workingDays = previewDays.filter(function (day) {
    return day.totalMinutes > 0;
  }).length;
  var weeklyMinutes = previewDays.reduce(function (sum, day) {
    return sum + day.totalMinutes;
  }, 0);
  return /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-preview"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-card"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-label"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.working-days')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-value"
  }, workingDays), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-unit"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.days-per-week'))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-card"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-label"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.weekly-hours')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-value"
  }, formatWeeklyHours(weeklyMinutes)), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-summary-unit"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.hours')))), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-table"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-table-head"
  }, /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-table-cell"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.table.day')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-table-cell"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.table.shift-times')), /*#__PURE__*/React.createElement("div", {
    className: "saas-attendance-group-settings__shift-table-cell"
  }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.table.duration'))), previewDays.map(function (day) {
    return /*#__PURE__*/React.createElement("div", {
      key: day.weekday,
      className: "saas-attendance-group-settings__shift-table-row"
    }, /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-table-cell"
    }, getWeekdayText(day.weekday)), /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-table-cell"
    }, day.timeRanges.length > 0 ? /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-times"
    }, day.timeRanges.map(function (range, index) {
      return /*#__PURE__*/React.createElement("div", {
        key: "".concat(day.weekday, "-").concat(index),
        className: "saas-attendance-group-settings__shift-time-tag"
      }, /*#__PURE__*/React.createElement("span", {
        className: "saas-attendance-group-settings__shift-time-text"
      }, range.startText), /*#__PURE__*/React.createElement("span", {
        className: "saas-attendance-group-settings__shift-time-arrow"
      }, "\u2192"), /*#__PURE__*/React.createElement("span", {
        className: "saas-attendance-group-settings__shift-time-text"
      }, range.endText));
    })) : /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-empty"
    }, locales.getText('pisell2.attendance.group-settings.shift-schedule.preview.no-shift'))), /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-table-cell"
    }, day.totalMinutes > 0 ? /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-duration"
    }, formatDuration(day.totalMinutes)) : /*#__PURE__*/React.createElement("div", {
      className: "saas-attendance-group-settings__shift-duration-empty"
    }, formatDuration(0))));
  })));
};
export default ShiftPreview;