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/pisellReservation/data/bookingCalendarMoveIntegration.ts
var bookingCalendarMoveIntegration_exports = {};
__export(bookingCalendarMoveIntegration_exports, {
  calendarBookingLikeToModalItem: () => calendarBookingLikeToModalItem,
  calendarBookingsToModalItems: () => calendarBookingsToModalItems,
  openBookingChangeModalAfterCalendarDrag: () => openBookingChangeModalAfterCalendarDrag
});
module.exports = __toCommonJS(bookingCalendarMoveIntegration_exports);
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_bookingChangeModal = __toESM(require("../components/bookingChangeModal"));
var import_utils = require("../../../utils");
function isHourInTimelineSlots(hour, hourSlots) {
  if (!hourSlots.length) return false;
  return hourSlots.some((x) => Math.abs(x - hour) < 1e-6);
}
function calendarBookingLikeToModalItem(like) {
  return {
    id: like.id,
    courtId: like.courtId,
    date: like.date,
    startHour: like.startHour,
    endHour: like.endHour,
    name: like.name,
    kind: like.kind,
    phone: like.phone,
    raw: like.raw
  };
}
function calendarBookingsToModalItems(list) {
  return list.map(calendarBookingLikeToModalItem);
}
function resourcesToCourts(resources) {
  return resources.map((r) => {
    const resource = r;
    return {
      id: String(resource.id),
      name: resource.name,
      form_id: String(resource.form_id ?? resource.id),
      relation_id: String(resource.id),
      resource_id: String(resource.id),
      relation_type: "form"
    };
  });
}
function resolveChildBookingOrderPathId(item) {
  const raw = item.raw;
  if (!raw) return null;
  const candidates = [raw.order_id, raw.booking_uid, raw.booking_id, raw.id];
  for (const c of candidates) {
    if (c !== void 0 && c !== null && String(c).trim() !== "") {
      return typeof c === "number" ? c : String(c);
    }
  }
  return null;
}
async function openBookingChangeModalAfterCalendarDrag(options) {
  const {
    calendarItem,
    draft,
    calendarResources,
    calendarBookings,
    effectiveTimelineRange,
    anchorDate,
    slotStepMinutes,
    onSuccessRefresh
  } = options;
  if (calendarItem.kind !== "booking") {
    return false;
  }
  const item = calendarBookingLikeToModalItem(calendarItem);
  const courts = resourcesToCourts(calendarResources);
  const modalBookings = calendarBookingsToModalItems(calendarBookings);
  const hourSlots = (0, import_materials.buildCalendarTimelineSlotStartsFromRange)(
    effectiveTimelineRange,
    anchorDate,
    slotStepMinutes
  );
  const slotStepHours = slotStepMinutes / 60;
  const firstTimelineHour = hourSlots[0] ?? 0;
  const endTimelineExclusive = hourSlots.length ? hourSlots[hourSlots.length - 1] + slotStepHours : 24;
  try {
    await import_bookingChangeModal.default.open({
      item,
      targetCourtId: draft.targetCourtId,
      targetDate: draft.targetDate,
      targetHour: draft.targetHour,
      courts,
      visibleCourts: courts,
      bookings: modalBookings,
      hourSlots,
      firstTimelineHour,
      endTimelineExclusive,
      theme: "dark",
      title: "确认调整预约",
      description: "拖拽后请确认场地、日期与时段，提交后生效。",
      confirmText: "确认改约",
      cancelText: "取消",
      getBookingDetailData: (b, courtList) => {
        var _a;
        return {
          customer: {
            name: b.name || "—",
            phone: typeof b.phone === "string" ? b.phone : "",
            email: ""
          },
          headerSummary: {
            customerName: b.name || "",
            resourceLabel: ((_a = courtList.find((c) => c.id === b.courtId)) == null ? void 0 : _a.name) || b.courtId
          }
        };
      },
      isBusinessHour: (h) => isHourInTimelineSlots(h, hourSlots),
      hasConflict: (allBookings, targetCourtId, targetDate, startHour, endHour, excludeId) => allBookings.some((booking) => {
        if (booking.id === excludeId) return false;
        if (booking.courtId !== targetCourtId || booking.date !== targetDate)
          return false;
        return !(endHour <= booking.startHour || startHour >= booking.endHour);
      }),
      onSubmit: async (ctx) => {
        const pathId = resolveChildBookingOrderPathId(ctx.item);
        if (pathId == null) {
          throw new Error("无法解析订单编号，请从预约详情改约");
        }
        await import_utils.request.getRequest().put(
          `/shop/v1.1/order/appointment/child-booking/${pathId}`,
          ctx.submitPayload
        );
        await onSuccessRefresh();
        import_antd.message.success("改约已保存");
      }
    });
    return true;
  } catch {
    return false;
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  calendarBookingLikeToModalItem,
  calendarBookingsToModalItems,
  openBookingChangeModalAfterCalendarDrag
});
