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 React, { useCallback, useEffect, useMemo, useState } from 'react';
import dayjs from 'dayjs';
import { Skeleton } from 'antd';
import { locales } from '@pisell/utils';
import CompensatedPisellContainer from "../../../pro/compensatedPisellContainer";
import { PisellReservationCore } from "../PisellReservation";
import { PISELL_RESERVATION_PAGE_LOCALES } from "../reservationConstants";
import useEngineContext from "../../../hooks/useEngineContext";
import "./SelectFloorMapResourceDialog.less";
var I18N_TITLE = 'pisell-reservation.select-resource.title';
var DIALOG_WIDTH = '99%';
var DIALOG_BODY_STYLE = {
  padding: 0,
  height: '100%'
};
var CONTENT_RENDER_DELAY = 200;
function SelectFloorMapResourceSkeleton() {
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-select-floor-map-resource-dialog-skeleton"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-select-floor-map-resource-dialog-skeleton__toolbar"
  }, /*#__PURE__*/React.createElement(Skeleton.Button, {
    active: true,
    block: true
  }), /*#__PURE__*/React.createElement(Skeleton.Button, {
    active: true,
    block: true
  }), /*#__PURE__*/React.createElement(Skeleton.Button, {
    active: true,
    block: true
  })));
}
function normalizeScheduleValue(value) {
  if (value == null) return undefined;
  var rawAt = typeof value === 'string' ? value : value.at;
  var at = dayjs(rawAt);
  if (!at.isValid()) return undefined;
  var rawDate = typeof value === 'string' ? undefined : value.date;
  var parsedDate = dayjs(rawDate);
  var date = rawDate != null && parsedDate.isValid() ? parsedDate.startOf('day') : at.startOf('day');
  return {
    date: date,
    at: at
  };
}
export function SelectFloorMapResourceDialog(_ref) {
  var _engineCtx$engine;
  var open = _ref.open,
    onClose = _ref.onClose,
    onClick = _ref.onClick,
    reservationBiz = _ref.reservationBiz,
    businessCode = _ref.businessCode,
    associatedMenuListIds = _ref.associatedMenuListIds,
    scheduleValue = _ref.scheduleValue,
    floorPlanId = _ref.floorPlanId,
    floorPlanCode = _ref.floorPlanCode,
    floorPlanCanvasWidth = _ref.floorPlanCanvasWidth,
    floorPlanCanvasHeight = _ref.floorPlanCanvasHeight,
    dataSources = _ref.dataSources,
    floorMap = _ref.floorMap,
    title = _ref.title;
  var engineCtx = useEngineContext();
  var engineLocale = (engineCtx === null || engineCtx === void 0 || (_engineCtx$engine = engineCtx.engine) === null || _engineCtx$engine === void 0 || (_engineCtx$engine = _engineCtx$engine.props) === null || _engineCtx$engine === void 0 ? void 0 : _engineCtx$engine.locale) || 'zh-CN';
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    shouldRenderContent = _useState2[0],
    setShouldRenderContent = _useState2[1];
  locales.init(PISELL_RESERVATION_PAGE_LOCALES, engineLocale);
  useEffect(function () {
    if (!open) {
      setShouldRenderContent(false);
      return undefined;
    }
    var timer = window.setTimeout(function () {
      setShouldRenderContent(true);
    }, CONTENT_RENDER_DELAY);
    return function () {
      window.clearTimeout(timer);
    };
  }, [open]);
  var handleSelectResource = useCallback(function (payload) {
    onClick === null || onClick === void 0 || onClick(payload);
  }, [onClick]);
  var resourceSelectMode = useMemo(function () {
    return {
      enabled: true,
      onSelectResource: handleSelectResource
    };
  }, [handleSelectResource]);
  var normalizedScheduleValue = useMemo(function () {
    return normalizeScheduleValue(scheduleValue);
  }, [scheduleValue]);
  return /*#__PURE__*/React.createElement(CompensatedPisellContainer, {
    renderType: "modal",
    props: {
      height: "99%",
      platform: 'pc',
      open: open,
      onCancel: onClose,
      title: title !== null && title !== void 0 ? title : locales.getText(I18N_TITLE),
      footer: null,
      destroyOnClose: true,
      width: DIALOG_WIDTH,
      className: 'pisell-select-floor-map-resource-dialog-modal',
      bodyStyle: DIALOG_BODY_STYLE,
      wrapClassName: 'pisell-select-floor-map-resource-dialog'
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-select-floor-map-resource-dialog-content"
  }, shouldRenderContent ? /*#__PURE__*/React.createElement(PisellReservationCore, {
    reservationBiz: reservationBiz !== null && reservationBiz !== void 0 ? reservationBiz : businessCode,
    associatedMenuListIds: associatedMenuListIds,
    scheduleValue: normalizedScheduleValue,
    floorPlanId: floorPlanId,
    floorPlanCode: floorPlanCode,
    floorPlanCanvasWidth: floorPlanCanvasWidth,
    floorPlanCanvasHeight: floorPlanCanvasHeight,
    dataSources: dataSources,
    floorMap: floorMap,
    bodyViewSwitchable: false,
    lockedBodyView: "floorMap",
    resourceSelectMode: resourceSelectMode
  }) : /*#__PURE__*/React.createElement(SelectFloorMapResourceSkeleton, null)));
}
export default SelectFloorMapResourceDialog;