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/editBookingModal/serve.ts
var serve_exports = {};
__export(serve_exports, {
  _editBooking: () => _editBooking,
  _getBookingDetail: () => _getBookingDetail,
  _getResources: () => _getResources,
  createCacheItem: () => createCacheItem,
  getBookingDetail: () => getBookingDetail,
  getChildrenBookingById: () => getChildrenBookingById,
  getResources: () => getResources,
  init: () => init,
  updateChildBooking: () => updateChildBooking,
  updateResourceErrors: () => updateResourceErrors
});
module.exports = __toCommonJS(serve_exports);
var import_utils = require("../../../utils");
var import_ticketBooking = require("../../ticketBooking/components/ticketBooking");
var import_dayjs = __toESM(require("dayjs"));
var import_utils2 = require("../info2/service/addService/utils");
var import_utils3 = require("../info2/utils");
var import_utilsByBooking = require("../info2/utilsByBooking");
var getBookingDetail = async (bookingId, params) => {
  let { data } = await import_utils.request.getRequest().get(`/shop/order/appointment/${bookingId}`, params);
  return data;
};
var getResources = async (params) => {
  let { data } = await import_utils.request.getRequest().get(`/shop/schedule/resource/list`, params);
  return data;
};
var updateChildBooking = async (orderId, params) => {
  let { data } = await import_utils.request.getRequest().put(`/shop/v1.1/order/appointment/child-booking/${orderId}`, params);
  return data;
};
var _getBookingDetail = async (bookingId) => {
  let data = await getBookingDetail(bookingId);
  return data;
};
var getChildrenBookingById = (bookings, id) => {
  return bookings.find((item) => item.booking_id === id);
};
var _getResources = async (params, bookingId) => {
  let data = await getResources(params);
  return (0, import_utils3.getFormatResourceListAndMap)(data, bookingId);
};
var _editBooking = async (data, values) => {
  console.log("submit data", data);
  console.log("submit values", values);
};
var updateResourceErrors = (resourceMaps, cacheItem) => {
  var _a, _b;
  if ((_a = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _a.resource) {
    let _resource = { ...(_b = cacheItem == null ? void 0 : cacheItem._extend) == null ? void 0 : _b.resource };
    for (let form_id in _resource) {
      _resource[form_id] = _resource[form_id].map((d) => {
        var _a2, _b2;
        return {
          ...d,
          errors: (0, import_utilsByBooking.getErrorList)(
            (_b2 = (_a2 = resourceMaps == null ? void 0 : resourceMaps[form_id]) == null ? void 0 : _a2.renderListMaps) == null ? void 0 : _b2[d == null ? void 0 : d.relation_id],
            cacheItem
          )
        };
      });
    }
    return _resource;
  }
};
function normalizeTargetResourceSnapshot(snapshot, fallbackId) {
  if (!snapshot) return void 0;
  const id = snapshot.id ?? fallbackId;
  if (id == null || String(id).trim() === "") return void 0;
  const mainField = snapshot.main_field ?? snapshot.code ?? String(id);
  return {
    ...snapshot,
    id,
    form_id: snapshot.form_id,
    main_field: mainField,
    metadata: snapshot.metadata ?? {},
    combined_resource: snapshot.combined_resource
  };
}
var init = async (params) => {
  var _a;
  const { toStartDate, toEndDate, event, fromResourceId, toResourceId } = params;
  let bookingOrigin = await _getBookingDetail(event.order.id);
  let booking = (0, import_ticketBooking._formatBookingDetail)(bookingOrigin, {});
  const resources = await _getResources({
    date: (0, import_dayjs.default)(toStartDate).format("YYYY-MM-DD")
  });
  let childrenBooking = getChildrenBookingById(booking.service.value, event.id);
  console.log("childrenBooking", childrenBooking);
  const fromResource = resources.listMap[fromResourceId];
  const fallbackToResource = normalizeTargetResourceSnapshot(
    params.toResource,
    toResourceId
  );
  if (!resources.listMap[toResourceId] && fallbackToResource) {
    resources.listMap[toResourceId] = fallbackToResource;
    resources.listMap[fallbackToResource.id] = fallbackToResource;
  }
  const toResource = resources.listMap[toResourceId];
  const extraParams = {
    extraResources: []
  };
  const _eventResources = (_a = childrenBooking._extend) == null ? void 0 : _a.event_resources;
  if (Array.isArray(_eventResources)) {
    _eventResources.forEach((item) => {
      extraParams.extraResources.push({
        id: item.relation_id,
        form_id: item.form_id
      });
    });
  }
  if (toResource) {
    extraParams.extraResources.push({
      form_id: toResource.form_id,
      id: toResource.id
    });
  }
  const resourcesOrigin = (0, import_utils2.getResourceByIds)(
    resources.listMap,
    childrenBooking,
    extraParams
  );
  return {
    // 原接口数据
    bookingOrigin,
    //格式化后的booking数据
    booking,
    // 当前子预约
    childrenBooking,
    // 时间范围数据
    dataRange: {
      end_date: childrenBooking._extend.end_date,
      start_date: childrenBooking._extend.start_date
    },
    // 总资源列表
    resourcesOrigin,
    // 源资源
    fromResource,
    // 目标资源
    toResource
  };
};
var createCacheItem = (cacheItem, values) => {
  var _a;
  console.log("values", values);
  if (!values || ((_a = values == null ? void 0 : values.toDate) == null ? void 0 : _a.length) !== 2 || !(values == null ? void 0 : values.resource)) {
    return cacheItem;
  }
  const { toDate, resource } = values || {};
  return {
    ...cacheItem,
    _extend: {
      ...cacheItem._extend,
      endDate: toDate[1],
      end_date: toDate[1],
      end_time: toDate[1].format("HH:mm"),
      startDate: toDate[0],
      start_date: toDate[0],
      start_time: toDate[0].format("HH:mm"),
      resource,
      sub_type: cacheItem._extend.sub_type || "minutes"
    }
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  _editBooking,
  _getBookingDetail,
  _getResources,
  createCacheItem,
  getBookingDetail,
  getChildrenBookingById,
  getResources,
  init,
  updateChildBooking,
  updateResourceErrors
});
