"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HotelOverridesContextWrapper = exports.HotelOverridesContext = void 0;
const contexts_1 = require("./index.js");
const hooks_1 = require("../hooks/index.js");
const i18next_1 = __importDefault(require("i18next"));
const react_1 = __importStar(require("react"));
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const BookingAPI_1 = __importDefault(require("../api/BookingAPI"));
const DistanceUnitType_types_1 = require("../models/Client/Hotel/DistanceUnitType.types");
const LanguageProvider_1 = require("../providers/LanguageProvider");
exports.HotelOverridesContext = (0, react_1.createContext)({});
const useHotelOverrides = () => {
    const companyContext = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const { t } = (0, react_i18next_1.useTranslation)();
    const [loading, loadingSet] = (0, react_1.useState)(false);
    const [overrides, overridesSet] = (0, react_1.useState)(null);
    const fetchApiData = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
        if (companyContext.selectedHotelID) {
            loadingSet(true);
            try {
                const apiData = yield BookingAPI_1.default.Hotel.fetchOverrides(companyContext.selectedHotelID);
                loadingSet(false);
                overridesSet(apiData);
            }
            catch (error) {
                console.error('Failed to fetch hotel overrides', error);
            }
        }
    }), [companyContext.selectedHotelID]);
    const hideByLine = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.hideByLine) || !!(hotel === null || hotel === void 0 ? void 0 : hotel.hideByLine);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.hideByLine]);
    const distanceUnitType = (0, react_1.useMemo)(() => {
        // Metric by default
        return ((overrides === null || overrides === void 0 ? void 0 : overrides.overrideDistanceUnitType) && (overrides === null || overrides === void 0 ? void 0 : overrides.distanceUnitType)) || (hotel === null || hotel === void 0 ? void 0 : hotel.distanceUnitType) || DistanceUnitType_types_1.DistanceUnitType.Metric;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.distanceUnitType, hotel === null || hotel === void 0 ? void 0 : hotel.distanceUnitType, hotel === null || hotel === void 0 ? void 0 : hotel.hasLoadedDetails]);
    const transportDistances = (0, react_1.useMemo)(() => {
        var _a;
        return (_a = overrides === null || overrides === void 0 ? void 0 : overrides.transportDistances) !== null && _a !== void 0 ? _a : hotel === null || hotel === void 0 ? void 0 : hotel.transportDistances;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.transportDistances]);
    const phoneNumberInSummary = (0, react_1.useMemo)(() => {
        return overrides === null || overrides === void 0 ? void 0 : overrides.phoneNumberInSummary;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.phoneNumberInSummary]);
    const enabledEventIds = (0, react_1.useMemo)(() => {
        return overrides === null || overrides === void 0 ? void 0 : overrides.enabledEventIds;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.enabledEventIds]);
    const { showYearOnCalendar, yearDisplayOption } = (0, react_1.useMemo)(() => {
        return {
            showYearOnCalendar: !!(overrides === null || overrides === void 0 ? void 0 : overrides.showYearOnCalendar),
            yearDisplayOption: !!(overrides === null || overrides === void 0 ? void 0 : overrides.showYearOnCalendar) ? overrides === null || overrides === void 0 ? void 0 : overrides.yearDisplayOption : '',
        };
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.showYearOnCalendar, overrides === null || overrides === void 0 ? void 0 : overrides.yearDisplayOption]);
    const { showPromoCodeAlert, promoCodeAlertType } = (0, react_1.useMemo)(() => {
        return {
            showPromoCodeAlert: !!(overrides === null || overrides === void 0 ? void 0 : overrides.showPromoCodeAlert),
            promoCodeAlertType: !!(overrides === null || overrides === void 0 ? void 0 : overrides.showPromoCodeAlert) ? overrides === null || overrides === void 0 ? void 0 : overrides.promoCodeAlertType : undefined,
        };
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.showPromoCodeAlert, overrides === null || overrides === void 0 ? void 0 : overrides.promoCodeAlertType]);
    const pageTitle = (0, react_1.useMemo)(() => {
        return overrides === null || overrides === void 0 ? void 0 : overrides.pageTitle;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.pageTitle]);
    const showPromoCodeDescription = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.showPromoCodeDescription);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.showPromoCodeDescription]);
    const alwaysShowDecimals = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.alwaysShowDecimals);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.alwaysShowDecimals]);
    const roomModalDescSideBySide = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.roomModalDescSideBySide);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.roomModalDescSideBySide]);
    const disableGroupRoomsAllTab = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.disableGroupRoomsAllTab);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.disableGroupRoomsAllTab]);
    const orderUnavailableRoomsLast = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.orderUnavailableRoomsLast);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.orderUnavailableRoomsLast]);
    const showPromocodeFieldInCheckout = (0, react_1.useMemo)(() => {
        return !!(overrides === null || overrides === void 0 ? void 0 : overrides.showPromocodeFieldInCheckout);
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.showPromocodeFieldInCheckout]);
    const messages = (0, react_1.useMemo)(() => {
        var _a;
        if ((_a = overrides === null || overrides === void 0 ? void 0 : overrides.messages) === null || _a === void 0 ? void 0 : _a.enable) {
            if (overrides.messages.pricesIncludeFeesMessage) {
                i18next_1.default.addResource(LanguageProvider_1.Language.en_us, 'translation', Translation_1.Translation.Step.Room.RoomInfo.IncludesTaxes, overrides.messages.pricesIncludeFeesMessage);
            }
            return overrides.messages;
        }
        return undefined;
    }, [overrides === null || overrides === void 0 ? void 0 : overrides.messages]);
    const getPricesIncludeFeesMessage = (hotelId) => __awaiter(void 0, void 0, void 0, function* () {
        var _a;
        if (hotelId === (hotel === null || hotel === void 0 ? void 0 : hotel.hotelID)) {
            const translated = t(Translation_1.Translation.Step.Room.RoomInfo.IncludesTaxes);
            return translated;
        }
        try {
            const overrideData = yield BookingAPI_1.default.Hotel.fetchOverrides(hotelId);
            const msg = ((_a = overrideData === null || overrideData === void 0 ? void 0 : overrideData.messages) === null || _a === void 0 ? void 0 : _a.pricesIncludeFeesMessage) || DefaultPricesIncludeFeesMessage;
            return msg;
        }
        catch (_b) {
            return DefaultPricesIncludeFeesMessage;
        }
    });
    (0, react_1.useEffect)(() => {
        fetchApiData();
    }, [fetchApiData]);
    return {
        loading,
        hideByLine,
        distanceUnitType,
        transportDistances,
        phoneNumberInSummary,
        enabledEventIds,
        showYearOnCalendar,
        yearDisplayOption,
        showPromoCodeAlert,
        promoCodeAlertType,
        pageTitle,
        showPromoCodeDescription,
        alwaysShowDecimals,
        messages,
        roomModalDescSideBySide,
        getPricesIncludeFeesMessage,
        disableGroupRoomsAllTab,
        orderUnavailableRoomsLast,
        showPromocodeFieldInCheckout,
    };
};
const HotelOverridesContextWrapper = ({ children }) => {
    const hotelOverrides = useHotelOverrides();
    return react_1.default.createElement(exports.HotelOverridesContext.Provider, { value: hotelOverrides }, children);
};
exports.HotelOverridesContextWrapper = HotelOverridesContextWrapper;
const DefaultPricesIncludeFeesMessage = 'Including taxes & fees.';
//# sourceMappingURL=HotelOverridesContext.js.map