var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

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