import React, { useCallback, useMemo } from 'react';
import dayjs from 'dayjs';
import { PisellModal } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { PisellReservationCore } from "../PisellReservation";
import "./SelectFloorMapResourceDialog.less";
var I18N_TITLE = 'pisell-reservation.select-resource.title';
var DIALOG_WIDTH = '99%';
var DIALOG_BODY_STYLE = {
  padding: 0,
  height: '100%'
};
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 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 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(PisellModal, {
    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"
  }, /*#__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
  })));
}
export default SelectFloorMapResourceDialog;