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/components/schedules/products/utils.ts
var utils_exports = {};
__export(utils_exports, {
  formatBackgroundColors: () => formatBackgroundColors,
  formatProductListByCategory: () => formatProductListByCategory,
  formatProductSchedules: () => formatProductSchedules,
  getMinTimeRange: () => getMinTimeRange,
  getMinTimeRangeBySchedule: () => getMinTimeRangeBySchedule,
  getMinTimeRangeStr: () => getMinTimeRangeStr,
  getProductMinTimeRangeStr: () => getProductMinTimeRangeStr,
  getScheduleColors: () => getScheduleColors,
  getShopOpeningHoursRange: () => getShopOpeningHoursRange,
  getUsableSchedules: () => getUsableSchedules,
  updateProductById: () => updateProductById
});
module.exports = __toCommonJS(utils_exports);
var import_dayjs = __toESM(require("dayjs"));
var import_utils = require("@pisell/utils");
var formatProductListByCategory = (list, filterItem) => {
  let _list = list || [];
  if (filterItem) {
    _list = _list.filter(filterItem);
  }
  let categoryObject = {};
  for (let i = 0; i < _list.length; i++) {
    const productItem = _list[i];
    if ((productItem.category || []).length) {
      for (let j = 0; j < productItem.category.length; j++) {
        let categoryItem = productItem.category[j];
        if (!categoryObject[categoryItem.id]) {
          categoryObject[categoryItem.id] = {
            ...categoryItem,
            products: []
          };
        }
        categoryObject[categoryItem.id] = {
          ...categoryItem,
          products: [
            ...categoryObject[categoryItem.id].products,
            productItem
          ]
        };
      }
    } else {
      if (!categoryObject["other"]) {
        categoryObject["other"] = {
          id: 0,
          title: import_utils.locales.getText("pisell2.text.other"),
          name: import_utils.locales.getText("pisell2.text.other"),
          cover: "",
          products: []
        };
      }
      categoryObject["other"] = {
        ...categoryObject["other"],
        products: [...categoryObject["other"].products, productItem]
      };
    }
  }
  return Object.values(categoryObject);
};
var formatBackgroundColors = (colors) => {
  let _colors = Array.from(new Set(colors.map((color) => color == null ? void 0 : color.toLocaleLowerCase()) || [])).filter((d) => !!d);
  let colorStr = _colors.map((color, index) => {
    return `${color} ${index * 100 / _colors.length}% ${(index + 1) * 100 / _colors.length}%`;
  }).join(", ");
  if (colorStr) {
    return `conic-gradient(${colorStr})`;
  }
  return "";
};
var getScheduleColors = (schedules) => {
  return formatBackgroundColors(schedules.map((d) => d.color));
};
var getProductMinTimeRangeStr = ({
  schedules,
  product
}) => {
  let productScheduleIds = product["schedule.ids"] || [];
  let filterList = schedules.filter((d) => true);
  let times = [];
  for (let schedule of filterList) {
    times = times.concat(getMinTimeRangeBySchedule(schedule));
  }
  return getMinTimeRangeStr(getMinTimeRange(times));
};
var getMinTimeRange = (array) => {
  if (!array.length) {
    return {
      minTime: "",
      maxTime: ""
    };
  }
  let minTime = array[0].minTime;
  let maxTime = array[0].maxTime;
  for (let i = 1; i < array.length; i++) {
    let startTime = array[i].minTime;
    let endTime = array[i].maxTime;
    if (startTime.isBefore(minTime)) {
      minTime = startTime;
    }
    if (endTime.isAfter(maxTime)) {
      maxTime = endTime;
    }
  }
  return { minTime, maxTime };
};
var getShopOpeningHoursRange = () => {
  let operating_day_boundary = { time: "23:59", type: "end_time" };
  if (localStorage.getItem("shopOpeningHoursObject")) {
    operating_day_boundary = JSON.parse(localStorage.getItem("shopOpeningHoursObject") || "");
  }
  let time = (0, import_dayjs.default)(operating_day_boundary.time, "HH:mm");
  let type = operating_day_boundary.type;
  let startDate = (0, import_dayjs.default)(time);
  if (type === "end_time") {
    startDate = startDate.subtract(1, "days");
  }
  let startDay = (0, import_dayjs.default)(startDate);
  let endDate = (0, import_dayjs.default)(time).add(1, "days").subtract(1, "second");
  if (type === "end_time") {
    endDate = endDate.subtract(1, "days");
  }
  let endDay = (0, import_dayjs.default)(endDate);
  return {
    shopStartDate: startDay,
    shopEndDate: endDay,
    shopStartTime: startDay.format("HH:mm"),
    shopEndTime: endDay.format("HH:mm")
  };
};
var getMinTimeRangeStr = (range) => {
  if (!range.minTime || !range.maxTime) {
    return "";
  }
  if (range.minTime.format("HH:mm") === "00:00" && range.maxTime.format("HH:mm") === "23:59") {
    return import_utils.locales.getText("pisell2.text.schedules.all-day");
  }
  let { shopStartDate, shopEndDate } = getShopOpeningHoursRange();
  let minTime = "";
  let maxTime = "";
  if (range.minTime.format("YYYY-MM-DD HH:mm") === shopStartDate.format("YYYY-MM-DD HH:mm")) {
    minTime = "open";
  } else {
    minTime = range.minTime.format("hh:mm a");
  }
  if (range.maxTime.format("YYYY-MM-DD HH:mm") === shopEndDate.format("YYYY-MM-DD HH:mm")) {
    maxTime = "close";
  } else {
    maxTime = range.maxTime.format("hh:mm a");
  }
  return `${minTime} - ${maxTime}`;
};
var getUsableSchedules = (schedule) => {
  if (!schedule) {
    return true;
  }
  if (!schedule.minTime || !schedule.maxTime) {
    return false;
  }
  let currentDate = (0, import_dayjs.default)();
  if (currentDate.isAfter(schedule.maxTime)) {
    return false;
  }
  return true;
};
var getMinTimeRangeBySchedule = (item) => {
  let minTime = (0, import_dayjs.default)(), maxTime = (0, import_dayjs.default)();
  if (item.type === "standard") {
    minTime = (0, import_dayjs.default)(item.start_time);
    maxTime = (0, import_dayjs.default)(item.end_time);
  }
  if (item.type === "time-slots") {
    let minTimeRange = getMinTimeRange(item.time_slot.map((d) => ({
      startDate: (0, import_dayjs.default)(d.start_time, "HH:mm"),
      endDate: (0, import_dayjs.default)(d.end_time, "HH:mm")
    })));
    minTime = minTimeRange.minTime;
    maxTime = minTimeRange.maxTime;
  }
  if (item.type === "designation") {
    let minTimeRange = getMinTimeRange(item.designation.map((d) => ({
      startDate: (0, import_dayjs.default)(d.start_date + " " + d.start_time),
      endDate: (0, import_dayjs.default)(d.end_date + " " + d.end_time)
    })));
    minTime = minTimeRange.minTime;
    maxTime = minTimeRange.maxTime;
  }
  return {
    startDate: minTime,
    endDate: maxTime
  };
};
var formatProductItemSchedules = (item, scheduleMap) => {
  let _list = (item["schedule.ids"] || []).map((t) => {
    var _a, _b;
    return {
      id: t,
      minTime: (_a = scheduleMap[t]) == null ? void 0 : _a.minTime,
      maxTime: (_b = scheduleMap[t]) == null ? void 0 : _b.maxTime
    };
  }).filter((t) => !!t.minTime && !!t.maxTime);
  let total = getMinTimeRange(_list);
  let str = getMinTimeRangeStr(total);
  return {
    ...item,
    _schedules: {
      minTime: total.minTime,
      maxTime: total.maxTime,
      str
    }
  };
};
var formatProductSchedules = (products, scheduleMap) => {
  return products.map((d) => formatProductItemSchedules(d, scheduleMap));
};
var updateProductById = ({ products, scheduleMap, id, schedule }) => {
  return products.map((d) => {
    return d.id === id ? formatProductItemSchedules({ ...d, "schedule.ids": schedule }, scheduleMap) : d;
  });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  formatBackgroundColors,
  formatProductListByCategory,
  formatProductSchedules,
  getMinTimeRange,
  getMinTimeRangeBySchedule,
  getMinTimeRangeStr,
  getProductMinTimeRangeStr,
  getScheduleColors,
  getShopOpeningHoursRange,
  getUsableSchedules,
  updateProductById
});
