import React, { useEffect } from 'react';
import { isMobile, locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import { AppointmentBookingProvider } from "./context";
import Content from "./components/Content";
import localeTexts from "./locales";
import { useAppointmentBooking, contextUtils } from "./hooks";
import { formatCurrentUser } from "./utils";
import "./index.less";
var AppointmentBooking = function AppointmentBooking() {
  var _context$engine;
  var context = useEngineContext();
  locales.init(localeTexts, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || 'en');
  var utils = contextUtils();
  var appointmentBooking = useAppointmentBooking();
  useEffect(function () {
    var _appointmentBooking$o;
    var _ref = (appointmentBooking === null || appointmentBooking === void 0 || (_appointmentBooking$o = appointmentBooking.otherParams) === null || _appointmentBooking$o === void 0 ? void 0 : _appointmentBooking$o.productModuleParams) || {},
      bookingSubject = _ref.bookingSubject;

    // 预约主体是表单则不需要添加账户
    if ((bookingSubject === null || bookingSubject === void 0 ? void 0 : bookingSubject.type) === 'form') return;
    appointmentBooking.getAccounts().then(function (data) {
      if (!data.length) {
        var currentUser = formatCurrentUser(utils);
        appointmentBooking.addAccounts([currentUser]);
      } else if (data.length >= 1) {
        var meAccount = data === null || data === void 0 ? void 0 : data.find(function (d) {
          return !(d !== null && d !== void 0 && d.isGuest);
        });
        // 如果 length >= 1 找到非 guest 账号，检测这个账号是否跟 localStorage 里已登录的账号 id 一致
        // 如果不一致的话，则setLoginAccount ls 里的账号
        var _currentUser = formatCurrentUser(utils);
        if (_currentUser && meAccount.id !== _currentUser.id) {
          appointmentBooking.setLoginAccount(meAccount.id, _currentUser);
        }
      }
    });
  }, []);
  useEffect(function () {
    var setRealVH = function setRealVH() {
      // window.innerHeight 会返回视口真实的高度（不包括工具栏）
      var height = window.innerHeight;

      // 检查 URL 参数中是否有 fromTabbar
      var urlParams = new URLSearchParams(window.location.search);
      if (urlParams.has('fromTabbar')) {
        height = height - 54;
      }
      var vh = height * 0.01;
      document.documentElement.style.setProperty('--vh', "".concat(vh, "px"));
    };
    // 页面加载和窗口大小变化时都重新计算
    setRealVH();
    window.addEventListener('resize', setRealVH);
    return function () {
      window.removeEventListener('resize', setRealVH);
    };
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: "parallel-booking-wrap ".concat(isMobile() ? 'parallel-booking-mobile-wrap' : '')
  }, /*#__PURE__*/React.createElement(AppointmentBookingProvider, null, /*#__PURE__*/React.createElement(Content, null)));
};
export default AppointmentBooking;