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/booking/info/service/addService/utils.tsx
var utils_exports = {};
__export(utils_exports, {
  checkCacheItemResource: () => checkCacheItemResource,
  formatResources: () => formatResources,
  getDurationProps: () => getDurationProps,
  getFlexibleOption: () => getFlexibleOption,
  getMultiDay: () => getMultiDay,
  getProductExtend: () => getProductExtend,
  getResourceTitle: () => getResourceTitle,
  getTimeObj: () => getTimeObj
});
module.exports = __toCommonJS(utils_exports);
var import_react = __toESM(require("react"));
var import_utils = require("../../../utils");
var import_dayjs = __toESM(require("dayjs"));
var import_utils2 = require("@pisell/utils");
var import_AlertTriangle = __toESM(require("@pisell/icon/es/AlertTriangle"));
var import_utils3 = require("../../service2/utils");
var import_utils4 = require("../../utils");
var getDurationProps = ({
  cacheItem,
  useStartTime,
  currentResource,
  slice
}) => {
  var _a;
  let _cacheItem = cacheItem;
  return {
    startDate: (0, import_dayjs.default)(
      (0, import_dayjs.default)(_cacheItem._extend.start_date).format("YYYY-MM-DD ") + ((0, import_utils.getServeStartTime)(useStartTime) || "00:00")
    ),
    usableTime: (_a = currentResource == null ? void 0 : currentResource._extend) == null ? void 0 : _a.usableTime,
    duration: (0, import_utils.getDuration)(_cacheItem == null ? void 0 : _cacheItem.duration),
    locale: import_utils2.locales.locale,
    slice,
    selectProps: { size: "large" }
  };
};
var getFlexibleOption = ({
  cacheItem,
  useStartTime,
  currentResource,
  slice,
  durationProps
}) => {
  var _a, _b;
  const _durationProps = durationProps || getDurationProps({
    cacheItem,
    useStartTime,
    currentResource,
    slice
  });
  let _duration = _durationProps.duration;
  if (_duration === "flexible") {
    let endDate = (0, import_utils.getEndDate)(
      _durationProps.startDate,
      _duration,
      ((_a = _durationProps.usableTime) == null ? void 0 : _a.end_at) ? (0, import_dayjs.default)((_b = _durationProps.usableTime) == null ? void 0 : _b.end_at) : null
    );
    let timeValue = endDate.diff(_durationProps.startDate, "minute");
    return {
      label: import_utils2.locales.getText("pisell2.text.flexible"),
      value: import_utils.flexibleObj.create(timeValue)
    };
  }
  return null;
};
var getTimeObj = ({
  cacheItem,
  useStartTime,
  currentResource,
  state,
  useDuration,
  durationProps
}) => {
  var _a, _b, _c;
  const flexibleOption = getFlexibleOption({
    cacheItem,
    useStartTime,
    currentResource,
    state,
    durationProps
  });
  let obj = (0, import_utils.sliceDayIntoFiveMinutes)(
    (_c = (_b = (_a = state.bookingConfig) == null ? void 0 : _a.config) == null ? void 0 : _b.basic) == null ? void 0 : _c.service_time,
    import_utils2.locales.locale,
    (0, import_utils3.isDayBooking)(state)
  );
  let timeSlices = [...obj.timeSlices];
  let durationSlicesBasedOnTime = [...obj.durationSlicesBasedOnTime];
  if (flexibleOption) {
    durationSlicesBasedOnTime.unshift(flexibleOption);
  }
  if (useDuration && !durationSlicesBasedOnTime.some((d) => d.value === useDuration)) {
    durationSlicesBasedOnTime.unshift({
      label: (0, import_utils.getDiscountTime)(useDuration, (0, import_utils3.isDayBooking)(state)),
      value: useDuration,
      //@ts-ignore
      disabled: true
    });
  }
  if (useStartTime && useStartTime !== "now" && !timeSlices.some((d) => d.value === useStartTime)) {
    timeSlices.unshift({
      label: (0, import_dayjs.default)(useStartTime, "HH:mm").format("hh:mm A"),
      value: useStartTime
    });
  }
  timeSlices.unshift({
    label: import_utils2.locales.getText("pisell2.text.now"),
    value: "now"
  });
  return {
    ...obj,
    timeSlices,
    durationSlicesBasedOnTime
  };
};
var formatResources = ({ cacheItem, resourcesOrigin, isDay }) => {
  try {
    let _list = [...resourcesOrigin];
    const _cacheItem = cacheItem;
    _list = _list.map(
      (d) => (0, import_utils.formatResource)(
        d,
        _cacheItem,
        (item) => {
          return /* @__PURE__ */ import_react.default.createElement("div", { className: "resource-option" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "like-wrap" }), item.labelText, !item.usable && /* @__PURE__ */ import_react.default.createElement(import_AlertTriangle.default, { className: "resource-option-alert-icon" }));
        },
        isDay
      )
    );
    _list = _list.sort((a, b) => b.usable ? 1 : -1);
    return _list;
  } catch (err) {
    console.log("formatResourcesErr", err);
  }
  return [];
};
var checkCacheItemResource = ({ cacheItem, resources }) => {
  var _a, _b;
  let resourceId = cacheItem._extend.resource;
  if (resourceId || resourceId === 0) {
    if (!resources.find((d) => (d == null ? void 0 : d.form_record_id) === resourceId)) {
      resourceId = void 0;
    }
  } else {
    if (cacheItem.new) {
      if (resources.length <= 2) {
        resourceId = (_a = resources == null ? void 0 : resources[(resources == null ? void 0 : resources.length) - 1]) == null ? void 0 : _a.form_record_id;
      }
      if (resources.length > 2) {
        const usableList = resources.filter(
          (d) => d.usable && d.form_id !== 0
        );
        if (usableList.length === 1) {
          resourceId = (_b = usableList == null ? void 0 : usableList[0]) == null ? void 0 : _b.form_record_id;
        }
      }
    }
  }
  return resourceId;
};
var getProductExtend = ({ cacheItem, state }) => {
  if (!cacheItem.new && cacheItem._extend.endDate) {
    return cacheItem._extend;
  }
  const resourcesOrigin = state.service.resourcesOrigin || [];
  let currentResource = null;
  if (!cacheItem.new) {
    const resources2 = formatResources({ cacheItem, resourcesOrigin });
    let item = resources2.find((d) => {
      var _a;
      return (d == null ? void 0 : d.form_record_id) == ((_a = cacheItem._extend) == null ? void 0 : _a.resource);
    }) || {};
    if (item) {
      currentResource = item;
    }
  }
  const timeObj = getTimeObj({ cacheItem, currentResource, state });
  let _extend = (0, import_utils.getServerItemExtend)(state, cacheItem, timeObj);
  const resources = formatResources({
    cacheItem: { ...cacheItem, _extend },
    resourcesOrigin
  });
  _extend.resource = checkCacheItemResource({
    cacheItem: { ...cacheItem, _extend },
    resources
  });
  return _extend;
};
var getResourceTitle = ({ state, id }) => {
  var _a;
  const resources = ((_a = state == null ? void 0 : state.client) == null ? void 0 : _a.value) || [];
  if (!resources.length) return "";
  const resource = resources.find((d) => d.id === id);
  return (resource == null ? void 0 : resource.main_field) || "-";
};
var getMultiDay = async ({
  start_date,
  end_date,
  state,
  cacheItem
}) => {
  let _cacheItem = cacheItem || state.service.cacheItem;
  if ((0, import_utils3.isDayBookingProduct)(_cacheItem, state)) {
    const { multiDayPrices, price } = await (0, import_utils3.updateAppointmentServicePrice)(
      state,
      {
        start_date,
        end_date,
        product_id: _cacheItem.product_id
      }
    );
    _cacheItem._extend.multiDayPrices = (0, import_utils4.getMultiDayPrices)(multiDayPrices || []);
    if (!(multiDayPrices == null ? void 0 : multiDayPrices.length)) {
      _cacheItem._extend.price = price;
    }
    return _cacheItem;
  }
  return _cacheItem;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  checkCacheItemResource,
  formatResources,
  getDurationProps,
  getFlexibleOption,
  getMultiDay,
  getProductExtend,
  getResourceTitle,
  getTimeObj
});
