'use strict';

var tslib_es6 = require('./tslib.es6-2c2ed574.js');
var React = require('react');
var core = require('@hhgtech/icons/core');
var Spinner = require('./Spinner-84d3cdf0.js');
var index$3 = require('./index-1e5e2223.js');
var index$2 = require('./index-3d563586.js');
var index$5 = require('./index-a31ed3e1.js');
var index$1 = require('./index-3643ff1b.js');
var index$4 = require('./index-b0ce6f8f.js');
var miscTheme = require('./miscTheme.js');
var styled = require('@emotion/styled');
var utils = require('./utils-b92a8b1c.js');
var miscDefaultClassWrapper = require('./miscDefaultClassWrapper.js');
var classNames = require('classnames');
var index = require('./index-6ce6e395.js');
var togetherComponentGlobalContext = require('./utils-302bc65c.js');
var normalizeLink = require('./normalizeLink-20e290e8.js');
var constantsDomainLocales = require('./constantsDomainLocales.js');
var profileNavigation = require('./profileNavigation.js');
var navigationLogoutPopup = require('./navigationLogoutPopup.js');

function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n["default"] = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);
var styled__default = /*#__PURE__*/_interopDefault(styled);
var classNames__default = /*#__PURE__*/_interopDefault(classNames);

const BlueWhiteLogin = (props) => (React__namespace.createElement("svg", Object.assign({ width: "32", height: "32", viewBox: "0 0 32 32", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, props),
    React__namespace.createElement("rect", { y: "0.5", width: "31", height: "31", rx: "15.5", fill: "white" }),
    React__namespace.createElement("path", { d: "M15.4999 14.3797C16.9062 14.3797 18.0462 13.2397 18.0462 11.8334C18.0462 10.4271 16.9062 9.28711 15.4999 9.28711C14.0936 9.28711 12.9536 10.4271 12.9536 11.8334C12.9536 13.2397 14.0936 14.3797 15.4999 14.3797Z", stroke: "#2743AD", strokeWidth: "1.2", strokeLinecap: "round", strokeLinejoin: "round" }),
    React__namespace.createElement("path", { d: "M19.9092 22.0335C20.7055 21.7825 21.124 20.9029 20.786 20.1399C19.8879 18.1112 17.861 16.6946 15.4999 16.6946C13.1388 16.6946 11.112 18.1112 10.2138 20.1399C9.87585 20.9038 10.2944 21.7825 11.0907 22.0335C12.2249 22.3909 13.7258 22.7131 15.4999 22.7131C17.274 22.7131 18.7749 22.3909 19.9092 22.0335Z", stroke: "#2743AD", strokeWidth: "1.2", strokeLinecap: "round", strokeLinejoin: "round" })));

const UserPlain = (props) => (React__namespace.createElement("svg", Object.assign({ width: "20", height: "20", viewBox: "0 0 20 20", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, props),
    React__namespace.createElement("path", { d: "M9.99991 8.3797C11.4062 8.3797 12.5462 7.23969 12.5462 5.83341C12.5462 4.42712 11.4062 3.28711 9.99991 3.28711C8.59363 3.28711 7.45361 4.42712 7.45361 5.83341C7.45361 7.23969 8.59363 8.3797 9.99991 8.3797Z", stroke: "white", strokeWidth: "1.2", strokeLinecap: "round", strokeLinejoin: "round" }),
    React__namespace.createElement("path", { d: "M14.4092 16.0335C15.2055 15.7825 15.624 14.9029 15.286 14.1399C14.3879 12.1112 12.361 10.6946 9.99992 10.6946C7.63881 10.6946 5.61196 12.1112 4.71381 14.1399C4.37585 14.9038 4.79436 15.7825 5.59066 16.0335C6.72492 16.3909 8.22584 16.7131 9.99992 16.7131C11.774 16.7131 13.2749 16.3909 14.4092 16.0335Z", stroke: "white", strokeWidth: "1.2", strokeLinecap: "round", strokeLinejoin: "round" })));

const useNavConfig = ({ locale, projectType, siteType, enableBookingBtn, }) => {
    const config = React.useMemo(() => {
        const enableSearchBar = projectType === 'discover' ||
            projectType === 'together' ||
            projectType === 'marryBaby' ||
            (projectType === 'profile' && siteType === 'marryBaby');
        const enableNavCare = siteType !== 'marryBaby' &&
            projectType !== 'marryBaby' &&
            index.CARE_LOCALES.includes(locale);
        const enableNavCareBooking = enableBookingBtn &&
            projectType !== 'marryBaby' &&
            projectType === 'discover' &&
            siteType !== 'marryBaby' &&
            index.CARE_BOOKING_LOCALES.includes(locale);
        const enableNavTogether = projectType === 'marryBaby' || index.LOCALE_HAS_COMMUNITY_SITE.includes(locale);
        const enableNavDiscover = true;
        const enableNavHealthTools = true;
        const enableProfileMySaved = true;
        const enableNavEcom = siteType !== 'marryBaby' && index.ECOM_LOCALES.includes(locale);
        const enableProfileEcom = siteType !== 'marryBaby' && index.ECOM_LOCALES.includes(locale);
        const enableProfileMyHealth = true;
        const enableNavVideo = index.VIDEO_LOCALES.includes(locale);
        const enableNavClinityDownload = index.HAS_CLINITY_DOWNLOAD_SITE.includes(locale);
        const enableNavCartCare = index.LOCALE_HAS_CART_CARE_SITE.includes(locale);
        const baseUrl = normalizeLink.getCurrentBaseUrl(locale, siteType === 'marryBaby');
        let fixBaseUrl = baseUrl;
        if (typeof window !== 'undefined') {
            if (fixBaseUrl.includes('//id.'))
                fixBaseUrl = fixBaseUrl.replace('id.', '');
            if (fixBaseUrl.includes('//staging-id.'))
                fixBaseUrl = fixBaseUrl.replace('staging-id.', 'discover.');
        }
        const ssoUrl = normalizeLink.getCurrentSsoUrl(locale, siteType === 'marryBaby');
        const statics = (() => {
            return {
                HOME_PAGE_URL: fixBaseUrl,
                CARE_URL_FE: `${fixBaseUrl}/care`,
                TOGETHER_URL_FE: `${fixBaseUrl}/community`,
                VIDEO_URL_FE: `${fixBaseUrl}/videos`,
                SSO_URL: ssoUrl,
                TOGETHER_API: normalizeLink.getTogetherApiDomain(),
                DISCOVER_API: normalizeLink.getDiscoverApiDomain(locale),
            };
        })();
        return {
            enableSearchBar,
            enableNavCare,
            enableNavCareBooking,
            enableNavTogether,
            enableNavEcom,
            enableProfileEcom,
            enableNavVideo,
            enableProfileMyHealth,
            enableNavDiscover,
            enableNavHealthTools,
            enableProfileMySaved,
            statics,
            enableNavClinityDownload,
            enableNavCartCare,
        };
    }, []);
    return config;
};

const NavigationContext = React__namespace["default"].createContext({});
const NavigationProvider = (_a) => {
    var { children } = _a, props = tslib_es6.__rest(_a, ["children"]);
    const [currentUrl, setCurrentUrl] = React__namespace["default"].useState(() => {
        return (props.currentUrl ||
            `https://${props.siteType === 'marryBaby'
                ? togetherComponentGlobalContext.domainLocalesMarryBaby[props.locale || 'en-PH']
                : constantsDomainLocales.domainLocales[props.locale || 'en-PH']}${props.projectType === 'together'
                ? '/community'
                : props.projectType === 'ecom'
                    ? '/ecom'
                    : props.projectType === 'care'
                        ? '/care'
                        : ''}/`);
    });
    React.useEffect(() => {
        const update = () => {
            setCurrentUrl(window.location.href);
        };
        window.addEventListener('popstate', update);
        update();
        return () => {
            window.removeEventListener('popstate', update);
        };
    }, []);
    const configs = useNavConfig({
        locale: props.locale,
        projectType: props.projectType,
        siteType: props.siteType,
        enableBookingBtn: props.enableBookingBtn,
    });
    return (React__namespace["default"].createElement(NavigationContext.Provider, { value: Object.assign(Object.assign({}, props), { customClasses: props.customClasses || {}, configs: Object.assign(Object.assign({}, configs), props.configs), currentUrl: props.currentUrl || currentUrl }) }, children));
};

const useCSSFocusPopupHelper = (configs) => {
    const { allowBgScroll } = configs || {};
    const ref = React.useRef();
    return {
        containerRef: ref,
        onContainerFocus: () => {
            if (!allowBgScroll)
                document.documentElement.style.overflow = 'hidden';
        },
        onContainerBlur: () => {
            if (!allowBgScroll)
                document.documentElement.style.overflow = '';
        },
        onButtonMouseDown: (e) => {
            // this close popup when click, run after REact init
            if (ref.current &&
                (document.activeElement === ref.current ||
                    ref.current.contains(document.activeElement))) {
                ref.current.blur();
                document.activeElement.blur();
                e.preventDefault();
            }
        },
        onClose: () => {
            if (ref.current &&
                (document.activeElement === ref.current ||
                    ref.current.contains(document.activeElement))) {
                ref.current.blur();
                document.activeElement.blur();
            }
        },
    };
};

const StyledPopup = miscDefaultClassWrapper.setDefaultClass(styled__default["default"].div `
    position: absolute;
    right: 0;
    transition: all 0.5s ease-in-out;
    overflow-y: auto;

    &[data-open='true'] {
      z-index: 200;
      top: 120%;
      opacity: 1;
      visibility: visible;
    }

    &[data-open='false'] {
      display: none;
    }

    .popup-content {
      z-index: 111;
      height: 100%;
      border: 1px solid #0000001a;
      border-radius: 16px;
      border-top-left-radius: 0;
      border-top-right-radius: 0;
    }

    /* min-height: 415px; */
    ${utils.MediaQueries.mbUp} {
      width: 320px;
    }
    ${utils.MediaQueries.mbDown} {
      position: fixed;
      left: 0;
      width: 100%;
      padding-bottom: 70px;
    }
  `, 'hhgcomp-navprofilebutton-popup');
const StyledPopupWrapper = miscDefaultClassWrapper.setDefaultClass(styled__default["default"].div `
    position: fixed;
    z-index: 150;
    top: var(--header-bottom, 56px);
    left: 0;
    width: 100%;
    height: 100%;
    align-items: center;

    ${utils.MediaQueries.mbUp} {
      background-color: transparent;
      &[data-type='welcome'] {
        background: rgba(38, 38, 38, 0.3);
      }
      &[data-type='due-date'] {
        background: rgba(38, 38, 38, 0.3);
      }
    }

    ${utils.MediaQueries.mbDown} {
      background: rgba(38, 38, 38, 0.3);
    }

    &[data-type='welcome'] {
      top: 0;
    }

    &[data-open='false'] {
      display: none;
    }
  `, 'hhgcomp-navprofilebutton-popupwrapper');
const StyledAvatar = styled__default["default"].div `
  width: 36px;
  height: 36px;
  cursor: pointer;

  img {
    border-radius: 50%;
  }

  .avatar {
    width: 36px;
    height: 36px;
    object-fit: cover;
  }

  .avatar-default {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: ${miscTheme.theme.colors.secondaryBase};
    border-radius: 50%;
    color: white;
    font-size: 20px;
    font-weight: bold;
  }
`;
const StyledImageWrapper = styled__default["default"].div `
  display: inline-flex;
  position: relative;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
  cursor: pointer;

  .user-close-icon {
    display: none;
  }
  &:focus-within {
    .user-close-icon {
      display: block;
    }
    .user-avatar {
      display: none;
    }
    ${StyledPopup.classSelector} {
      z-index: 200;
      top: 120%;
      opacity: 1;
      visibility: visible;
      display: initial;
      ${utils.MediaQueries.mbDown} {
        top: 100%;
      }
    }
    ${StyledPopupWrapper.classSelector} {
      display: initial;
    }
  }
`;
const StyledLoggedOutAnchor = styled__default["default"].a `
  display: inline-flex;
  .login-btn[data-color='tertiary'][data-size='md'] {
    padding-left: 12px;
    padding-right: 12px;
  }
  .mobile-only {
    ${utils.MediaQueries.mbUp} {
      display: none;
    }
  }
  .login-btn {
    display: none;
    ${utils.MediaQueries.mbUp} {
      display: block;
      border: 1px solid ${miscTheme.theme.colors.neutral100};
    }
  }
  &[data-win-cash-back-theme='true'] {
    .login-btn {
      ${utils.MediaQueries.mbUp} {
        color: ${miscTheme.theme.colors.white};
      }
    }
  }
`;

const ProfilePopup = ({ isOpen, userInfo, closePopup, siteType = 'helloSites', dispatchLogout, }) => {
    const { customClasses = {}, configs: { enableProfileFindMyBooking }, } = React.useContext(NavigationContext);
    const handleClickLogout = () => {
        if (dispatchLogout) {
            dispatchLogout();
            closePopup();
        }
    };
    return (React__namespace["default"].createElement(StyledPopup, { className: customClasses.profilePopup, "data-open": isOpen },
        React__namespace["default"].createElement("div", { className: classNames__default["default"]('popup-content', customClasses.profilePopupContent) },
            React__namespace["default"].createElement(profileNavigation.ProfileNavigation, { siteType: siteType, onLogout: handleClickLogout, userInfo: userInfo, configs: { enableProfileFindMyBooking } }))));
};

const NavigationProfileButton = ({ userInfo, siteType, onLoginClick, currentUrl, isMinifyLogin, onLogout, style, isLoading, isMobile, className, }) => {
    const { isWinCashBackTheme: winCashBackTheme } = React.useContext(NavigationContext);
    const { t } = index$1.useTranslations();
    const [isLogoutConfirmOpen, setIsLogoutConfirmOpen] = React.useState(false);
    const name = (userInfo === null || userInfo === void 0 ? void 0 : userInfo.name) || (userInfo === null || userInfo === void 0 ? void 0 : userInfo.username);
    const { containerRef, onContainerFocus, onContainerBlur, onButtonMouseDown, onClose, } = useCSSFocusPopupHelper();
    const isLoginIcon = React.useMemo(() => isMobile && siteType === 'helloSites', [isMobile]);
    if (isLoading)
        return isLoginIcon ? (React__namespace["default"].createElement(Spinner.Spinner, null)) : (React__namespace["default"].createElement(index$2.Button, { size: 'sm', color: "transparent", loading: true }));
    return (userInfo === null || userInfo === void 0 ? void 0 : userInfo.id) ? (React__namespace["default"].createElement(React__namespace["default"].Fragment, null,
        React__namespace["default"].createElement(StyledImageWrapper, { ref: containerRef, style: style, tabIndex: 0, onBlur: onContainerBlur, onFocus: () => {
                var _a;
                onContainerFocus();
                const popup = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('#navigation-v2 .hhgcomp-navprofilebutton-popup');
                if (popup) {
                    const bound = popup.getBoundingClientRect();
                    const top = bound.top;
                    const maxHeight = `calc(100vh - ${top}px)`;
                    popup.setAttribute('style', `max-height: ${maxHeight};`);
                }
            }, className: className },
            React__namespace["default"].createElement("div", { onMouseDown: onButtonMouseDown },
                React__namespace["default"].createElement("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", className: "user-close-icon" },
                    React__namespace["default"].createElement("path", { d: "M18 6L6 18M18 18L6 6", stroke: "#3E3F58", strokeWidth: "1.3", strokeLinecap: "round" })),
                React__namespace["default"].createElement(index$3.Avatar, { size: 32, className: "user-avatar", src: userInfo === null || userInfo === void 0 ? void 0 : userInfo.avatar, color: siteType === 'marryBaby'
                        ? miscTheme.theme.mbColors.pink
                        : miscTheme.theme.colors.secondaryBase, placeholderText: name && name.length ? name[0].toUpperCase() : 'A' })),
            React__namespace["default"].createElement(ProfilePopup, { dispatchLogout: () => {
                    onClose();
                    setIsLogoutConfirmOpen(true);
                }, isOpen: false, closePopup: onClose, siteType: siteType, userInfo: userInfo })),
        React__namespace["default"].createElement(navigationLogoutPopup.NavigationLogoutPopup, { openLogoutPopup: isLogoutConfirmOpen, onCloseHandler: () => {
                document.body.classList.toggle('noscroll', false);
                setIsLogoutConfirmOpen(false);
            }, siteType: siteType, onLogoutHandler: () => tslib_es6.__awaiter(void 0, void 0, void 0, function* () {
                setIsLogoutConfirmOpen(false);
                document.body.classList.toggle('noscroll', false);
                yield onLogout();
            }) }))) : (React__namespace["default"].createElement(StyledLoggedOutAnchor, { style: Object.assign({ textDecoration: 'none' }, style), "data-event-category": "Sign Up", "data-event-action": "Button Click", "data-event-label": currentUrl, "data-win-cash-back-theme": winCashBackTheme, "data-site-type": siteType, onClick: onLoginClick }, isMinifyLogin ? (React__namespace["default"].createElement(StyledAvatar, null,
        React__namespace["default"].createElement("img", { src: index$4.CommonGAssets.getAssetPath('lock.png'), className: "avatar", loading: "lazy", alt: "avatar" }))) : (
    // LOGIC:
    // - DEsktop: always show button
    // - Mobile: show button only for helloSites, show text for MarryBaby
    React__namespace["default"].createElement(React__namespace["default"].Fragment, null,
        siteType === 'helloSites' &&
            (isLoginIcon ? (winCashBackTheme ? (React__namespace["default"].createElement(BlueWhiteLogin, { className: "mobile-only", style: {
                    cursor: 'pointer',
                    width: 32,
                    height: 32,
                    flexShrink: 0,
                } })) : (React__namespace["default"].createElement("img", { className: "mobile-only", src: index$4.CommonGAssets.getAssetPath('login.svg'), style: {
                    cursor: 'pointer',
                    height: 24,
                    width: 24,
                } }))) : (React__namespace["default"].createElement(index$2.Button, { className: "mobile-only", size: "sm", leftIcon: React__namespace["default"].createElement(core.User, { color: "currentColor", isSolid: true, size: 22 }), sx: {
                    paddingLeft: '8px',
                    paddingRight: '12px',
                }, styles: {
                    leftIcon: {
                        marginRight: 4,
                    },
                } }, t('navigation.loginJoin')))),
        siteType === 'marryBaby' && (React__namespace["default"].createElement("div", { className: "mobile-only", style: {
                paddingLeft: 16,
            } },
            React__namespace["default"].createElement(index$5.Text, { size: "p3", color: miscTheme.theme.mbColors.pink }, t('navigation.loginJoin')))),
        siteType === 'marryBaby' ? (React__namespace["default"].createElement(index$2.Button, { className: "login-btn", size: "md", leftIcon: React__namespace["default"].createElement(core.User, { color: "currentColor", isSolid: true, size: 22 }) }, t('navigation.loginJoin'))) : (React__namespace["default"].createElement(index$2.Button, { className: "login-btn", size: "md", leftIcon: winCashBackTheme ? (React__namespace["default"].createElement(UserPlain, { style: { flexShrink: 0, width: 20, height: 20 } })) : (React__namespace["default"].createElement(core.User, { color: "#595959", isSolid: true, size: 22 })), variant: "ghost", color: "#262626", sx: {
                fontWeight: 500,
                fontSize: 14,
                lineHeight: 22,
            } }, t('navigation.loginJoin')))))));
};

exports.BlueWhiteLogin = BlueWhiteLogin;
exports.NavigationContext = NavigationContext;
exports.NavigationProfileButton = NavigationProfileButton;
exports.NavigationProvider = NavigationProvider;
exports.UserPlain = UserPlain;
exports.useCSSFocusPopupHelper = useCSSFocusPopupHelper;
