function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useCallback, useState } from 'react';
import { findReservationRow } from "../floorMap/reservationCards";
import { resolveFloorMapHostCheckoutRecord } from "../floorMap/floorMapHostCheckout";

/** 与 useEngineContext 返回值形状一致（不直接 import hook），避免类型循环 */

/**
 * 只读平面图点击 → 宿主结账抽屉或内置预约详情弹窗的状态与 `floorMapItemClickBefore`。
 */
export function useFloorMapBookingInteraction(options) {
  var engineCtx = options.engineCtx,
    onFloorMapBookingClick = options.onFloorMapBookingClick;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    bookingDetailOpen = _useState2[0],
    setBookingDetailOpen = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    bookingDetailRaw = _useState4[0],
    setBookingDetailRaw = _useState4[1];
  var _useState5 = useState(null),
    _useState6 = _slicedToArray(_useState5, 2),
    bookingDetailRow = _useState6[0],
    setBookingDetailRow = _useState6[1];
  var _useState7 = useState(undefined),
    _useState8 = _slicedToArray(_useState7, 2),
    bookingDetailSceneName = _useState8[0],
    setBookingDetailSceneName = _useState8[1];
  var closeBookingDetailModal = useCallback(function (e) {
    var _e$nativeEvent, _e$nativeEvent$stopIm;
    e === null || e === void 0 || e.stopPropagation();
    e === null || e === void 0 || (_e$nativeEvent = e.nativeEvent) === null || _e$nativeEvent === void 0 || (_e$nativeEvent$stopIm = _e$nativeEvent.stopImmediatePropagation) === null || _e$nativeEvent$stopIm === void 0 || _e$nativeEvent$stopIm.call(_e$nativeEvent);
    window.setTimeout(function () {
      setBookingDetailOpen(false);
      setBookingDetailRaw(null);
      setBookingDetailRow(null);
      setBookingDetailSceneName(undefined);
    }, 50);
  }, []);
  var floorMapItemClickBefore = useCallback(function (args) {
    var _args$item$_sceneElem, _args$item$_sceneElem2, _args$item$_sceneElem3, _engineCtx$appHelper, _utils$getData, _utils$getData2, _utils$store, _utils$store$getState, _args$item$_sceneElem4;
    if (args.mode === 'edit') return;
    var rowHit = findReservationRow(args.item, args.dataSources);
    var list = rowHit === null || rowHit === void 0 ? void 0 : rowHit.floorMapSourceBookings;
    var rec = resolveFloorMapHostCheckoutRecord(args.event, list);
    if (!rec || _typeof(rec) !== 'object') return;
    var rid = rec.id;
    var bid = rec.booking_id;
    var hasId = rid != null && String(rid).trim() !== '' || bid != null && String(bid).trim() !== '';
    if (!hasId) return;
    var rowResolved = rowHit !== null && rowHit !== void 0 ? rowHit : {
      id: String((_args$item$_sceneElem = (_args$item$_sceneElem2 = args.item._sceneElement) === null || _args$item$_sceneElem2 === void 0 || (_args$item$_sceneElem2 = _args$item$_sceneElem2.dataBinding) === null || _args$item$_sceneElem2 === void 0 ? void 0 : _args$item$_sceneElem2.recordId) !== null && _args$item$_sceneElem !== void 0 ? _args$item$_sceneElem : 'unknown'),
      code: ((_args$item$_sceneElem3 = args.item._sceneElement) === null || _args$item$_sceneElem3 === void 0 ? void 0 : _args$item$_sceneElem3.name) != null ? String(args.item._sceneElement.name) : '—',
      cardStatus: 'available'
    };
    var consumed = onFloorMapBookingClick === null || onFloorMapBookingClick === void 0 ? void 0 : onFloorMapBookingClick({
      event: args.event,
      raw: rec,
      row: rowResolved,
      dataSources: args.dataSources,
      mode: args.mode
    });
    if (consumed === true) return;
    var utils = engineCtx === null || engineCtx === void 0 || (_engineCtx$appHelper = engineCtx.appHelper) === null || _engineCtx$appHelper === void 0 ? void 0 : _engineCtx$appHelper.utils;
    var shopId = (_utils$getData = utils === null || utils === void 0 || (_utils$getData2 = utils.getData) === null || _utils$getData2 === void 0 ? void 0 : _utils$getData2.call(utils, 'shop_id')) !== null && _utils$getData !== void 0 ? _utils$getData : utils === null || utils === void 0 || (_utils$store = utils.store) === null || _utils$store === void 0 || (_utils$store$getState = _utils$store.getState) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.call(_utils$store)) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.global) === null || _utils$store$getState === void 0 || (_utils$store$getState = _utils$store$getState.globalConfig) === null || _utils$store$getState === void 0 ? void 0 : _utils$store$getState.id;
    var n = Number(shopId);
    var isLegacyCheckoutShop = Number.isFinite(n) && n === 2155;
    if (isLegacyCheckoutShop) {
      var _utils$action;
      utils === null || utils === void 0 || (_utils$action = utils.action) === null || _utils$action === void 0 || _utils$action.call(utils, {
        type: 'pisell1.handleOpenHostCheckoutDrawer',
        data: {
          item: rec
        }
      });
      return;
    }
    var sceneName = ((_args$item$_sceneElem4 = args.item._sceneElement) === null || _args$item$_sceneElem4 === void 0 ? void 0 : _args$item$_sceneElem4.name) != null ? String(args.item._sceneElement.name).trim() : undefined;
    setBookingDetailSceneName(sceneName && sceneName !== '' ? sceneName : undefined);
    setBookingDetailRaw(rec);
    setBookingDetailRow(rowResolved);
    setBookingDetailOpen(true);
  }, [engineCtx, onFloorMapBookingClick]);
  return {
    bookingDetailOpen: bookingDetailOpen,
    bookingDetailRaw: bookingDetailRaw,
    bookingDetailRow: bookingDetailRow,
    bookingDetailSceneName: bookingDetailSceneName,
    closeBookingDetailModal: closeBookingDetailModal,
    floorMapItemClickBefore: floorMapItemClickBefore
  };
}