var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/booking/info/service/serviceManager/index.tsx
var serviceManager_exports = {};
__export(serviceManager_exports, {
  useService: () => useService
});
module.exports = __toCommonJS(serviceManager_exports);
var import_utils = require("@pisell/utils");
var import_react = require("react");
var import_utils2 = require("../../../utils");
var import_utils3 = require("../../service2/Group/utils");
var import_utils4 = require("../../service2/utils");
var import_utils5 = require("../addService/utils");
var lock = false;
var useService = ({
  renderMode,
  onEdit,
  state,
  dispatch
}) => {
  var _a;
  const holderId = (0, import_react.useMemo)(() => {
    var _a2;
    return Number(((_a2 = state == null ? void 0 : state.activeCartId) == null ? void 0 : _a2[0]) || 0);
  }, [state == null ? void 0 : state.activeCartId]);
  const isDayBookingMode = (0, import_react.useMemo)(() => {
    return (0, import_utils4.isDaysBooking)(state);
  }, [state == null ? void 0 : state.renderType]);
  const isEventBookingMode = (0, import_react.useMemo)(() => {
    return (0, import_utils4.isEventBooking)(state);
  }, [state == null ? void 0 : state.renderType]);
  const createBookingData = async ({ product }) => {
    var _a2, _b;
    if (lock) {
      return;
    }
    lock = true;
    setTimeout(() => {
      lock = false;
    }, 2e3);
    const { _params } = product;
    let cacheItem = {
      ...product,
      product_id: product.id,
      _id: (0, import_utils.getUniqueId)(),
      _key: _params.key,
      _extend: {
        start_date: state.date.value,
        quantity: 1,
        price: product.price,
        product_name: product.title,
        other: JSON.parse(JSON.stringify(_params))
      },
      new: 1
    };
    if (isDayBookingMode || isEventBookingMode) {
      cacheItem._extend.holder_id = holderId;
      cacheItem._extend.end_date = state.date.end_date;
      cacheItem._extend.sub_type = "days";
    }
    if ((0, import_utils2.isSessionProduct)(cacheItem) && _params.session) {
      cacheItem._extend.start_date = _params.session.start_at;
      cacheItem._extend.start_time = _params.session.start_time;
      cacheItem._extend.end_date = _params.session.end_at;
    }
    let _total = (0, import_utils2.getProductTotalPrice)(cacheItem);
    cacheItem._extend.total = _total;
    cacheItem._extend.origin_total = _total;
    const _isEdit = !!state.service.cacheItem;
    if (_isEdit) {
      cacheItem._id = (_a2 = state.service.cacheItem) == null ? void 0 : _a2._id;
      cacheItem.new = 0;
    }
    if (renderMode === "current") {
      cacheItem.currentDefaultTime = state.date.value.format("HH:mm");
    }
    cacheItem._extend = (0, import_utils5.getProductExtend)({ cacheItem, state });
    if (isDayBookingMode) {
      cacheItem = await (0, import_utils5.getMultiDay)({
        start_date: cacheItem._extend.startDate,
        end_date: cacheItem._extend.endDate,
        state,
        cacheItem
      });
      if ((0, import_utils4.isDayBookingProduct)(cacheItem, state)) {
        cacheItem = (0, import_utils3.concatBookings)((0, import_utils3.createBookingItemsByGroup)(cacheItem));
      }
    }
    if (isEventBookingMode) {
      cacheItem.autoClose = false;
    } else {
      if (isDayBookingMode && !(0, import_utils4.isDayBookingProduct)(cacheItem, state)) {
        cacheItem.autoClose = true;
      } else {
        cacheItem.autoClose = !_isEdit && (cacheItem._extend.resource || cacheItem._extend.resource === 0 || (((_b = state.service) == null ? void 0 : _b.resourcesOrigin) || []).length === 1);
      }
    }
    return cacheItem;
  };
  const currentResource = (0, import_react.useCallback)(
    (useResource) => {
      const list = state.service.resources;
      let item = list.find((d) => d.form_record_id == useResource) || {};
      return item;
    },
    [(_a = state == null ? void 0 : state.service) == null ? void 0 : _a.resources]
  );
  const editBookingData = async ({ changeValues }) => {
    var _a2;
    let _item = {
      ...state.service.cacheItem,
      autoClose: false
    };
    let _extend = {
      ..._item._extend,
      ...changeValues
    };
    if ((0, import_utils4.isDayBookingProduct)(state.service.cacheItem, state)) {
      _extend.quantity = 1;
      _extend.startDate = _extend == null ? void 0 : _extend.start_date;
      _extend.endDate = _extend == null ? void 0 : _extend.end_date;
    } else {
      const { startDate, endDate } = (0, import_utils2.getServiceTimes)({ _extend });
      _extend.startDate = startDate;
      _extend.endDate = endDate;
    }
    const resourceItem = currentResource(changeValues.resource);
    const { relation_type, item_id, schedule_id } = ((_a2 = resourceItem == null ? void 0 : resourceItem._extend) == null ? void 0 : _a2.usableTime) || {};
    _extend.relation_type = relation_type || (_extend == null ? void 0 : _extend.relation_type) || "form";
    _extend.relation_id = item_id || (resourceItem == null ? void 0 : resourceItem.form_record_id) || (_extend == null ? void 0 : _extend.relation_id) || 0;
    _extend.schedule_id = schedule_id || (_extend == null ? void 0 : _extend.schedule_id) || 0;
    if (_extend.bundle_edit !== void 0) {
      _extend.bundle_edit = 1;
    }
    _item._extend = _extend;
    _item._time = (/* @__PURE__ */ new Date()).getTime();
    return _item;
  };
  const createBooking = async ({ booking }) => {
    goEditPanel({ booking });
  };
  const editBooking = async ({ booking }) => {
    var _a2, _b;
    let _list = state.service.value;
    let _id = booking == null ? void 0 : booking._id;
    let groupId = (_a2 = booking == null ? void 0 : booking._extend) == null ? void 0 : _a2.groupId;
    if (groupId) {
      _list = _list.filter((d) => {
        var _a3;
        return ((_a3 = d._extend) == null ? void 0 : _a3.groupId) !== groupId;
      });
      _list = _list.concat((0, import_utils3.createBookingItemsByGroup)(booking, true));
    } else {
      if (!_list.some((d) => d._id === _id)) {
        if (!(0, import_utils4.isBookingProduct)(booking == null ? void 0 : booking.extension_type) || !isDayBookingMode) {
          _list.push(booking);
        } else {
        }
      } else {
        _list = _list.map((d) => d._id === _id ? { ...booking, new: 0 } : d);
      }
    }
    (0, import_utils2.deleteCacheParams)();
    (_b = window.closeBookingEditModal) == null ? void 0 : _b.call(window);
    dispatch({
      type: "setService",
      payload: {
        editModal: false,
        addModal: false,
        value: _list,
        edit: true,
        cacheItem: null,
        changeService: false
      }
    });
  };
  const goEditPanel = ({ booking }) => {
    var _a2;
    (_a2 = window.openBookingEditModal) == null ? void 0 : _a2.call(window);
    dispatch({
      type: "setService",
      payload: {
        editModal: true,
        cacheItem: booking,
        addModal: !booking.autoClose,
        changeService: false
      }
    });
    onEdit == null ? void 0 : onEdit(booking);
  };
  return {
    createBookingData,
    editBookingData,
    createBooking,
    editBooking,
    goEditPanel
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useService
});
