import { getIsWebCrawler } from "@insite/client-framework/Common/WebCrawler";
import { getEnableSsr } from "@insite/client-framework/ServerSideRendering";
import { FulfillmentMethod } from "@insite/client-framework/Services/SessionService";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getShipToState } from "@insite/client-framework/Store/Data/ShipTos/ShipTosSelectors";
import translate from "@insite/client-framework/Translate";
import DeferredApisModel from "@insite/client-framework/Types/DeferredApisModel";

export function getCurrencies(state: ApplicationState) {
    return state.context.website.currencies!.currencies;
}

export function getLanguages(state: ApplicationState) {
    return state.context.website.languages!.languages;
}

export function getSettingsCollection(state: ApplicationState) {
    return state.context.settings.settingsCollection;
}

export function getSelectedCategoryPath(state: ApplicationState) {
    return state.context.selectedCategoryPath;
}

export function getSelectedProductPath(state: ApplicationState) {
    return state.context.selectedProductPath;
}

export function getSelectedBrandPath(state: ApplicationState) {
    return state.context.selectedBrandPath;
}

export const getDefaultPageSize = (state: ApplicationState) =>
    state.context.settings.settingsCollection.websiteSettings.defaultPageSize ?? -1;

export const getCurrentUserIsGuest = (state: ApplicationState) =>
    state.context.session.isAuthenticated && state.context.session.isGuest;

export const getSession = (state: ApplicationState) => state.context.session;

export const getFulfillmentLabel = (state: ApplicationState) => {
    const { fulfillmentMethod, isAuthenticated, rememberMe, isGuest, shipToId, pickUpWarehouse } = getSession(state);
    const shipToState = getShipToState(state, shipToId);
    const { value: shipTo } = shipToState;
    let addressLabel = translate("Ship");
    if (fulfillmentMethod === FulfillmentMethod.Ship && (isAuthenticated || rememberMe) && !isGuest && shipTo) {
        addressLabel = translate("Ship to {0}", `${shipTo.companyName}, ${shipTo.fullAddress}`);
    } else if (fulfillmentMethod === FulfillmentMethod.PickUp) {
        addressLabel = translate(
            "Pick up {0}",
            pickUpWarehouse ? `at ${pickUpWarehouse.description || pickUpWarehouse.name}` : "",
        );
    }

    return addressLabel;
};

export const getIsPunchOutSession = (state: ApplicationState) => {
    return !!state.context.punchOutSessionId && !!state.context.session?.userName;
};

export const getSearchDataModeActive = (state: ApplicationState) => {
    return !!state.context.isSearchDataModeActive;
};

export const getShouldLoadCatalog = (state: ApplicationState) => {
    const { websiteSettings } = getSettingsCollection(state);
    if (getIsWebCrawler() || getEnableSsr()) {
        return true;
    }

    if (
        IS_SERVER_SIDE &&
        websiteSettings.enableServerSideRendering &&
        websiteSettings.includeCatalogInServerSideRenderingResponse
    ) {
        return true;
    }

    if (!IS_SERVER_SIDE) {
        return true;
    }

    return false;
};

export const getShouldLoadPricing = (state: ApplicationState) => {
    const { websiteSettings } = getSettingsCollection(state);
    if ((getIsWebCrawler() && websiteSettings.includeRealTimeInventoryAndPricingForSeo) || getEnableSsr()) {
        return true;
    }

    if (
        IS_SERVER_SIDE &&
        websiteSettings.enableServerSideRendering &&
        websiteSettings.includeRealTimePricingInServerSideRenderingResponse
    ) {
        return true;
    }

    if (!IS_SERVER_SIDE) {
        return true;
    }

    return false;
};

export const getShouldLoadInventory = (state: ApplicationState) => {
    const { websiteSettings } = getSettingsCollection(state);
    if ((getIsWebCrawler() && websiteSettings.includeRealTimeInventoryAndPricingForSeo) || getEnableSsr()) {
        return true;
    }

    if (
        IS_SERVER_SIDE &&
        websiteSettings.enableServerSideRendering &&
        websiteSettings.includeRealTimeInventoryInServerSideRenderingResponse
    ) {
        return true;
    }

    if (!IS_SERVER_SIDE) {
        return true;
    }

    return false;
};

export const getShouldPreventCatalogLoop = (deferredApisModel: DeferredApisModel) => {
    if (IS_SERVER_SIDE && deferredApisModel.catalog > 0) {
        return true;
    }

    return false;
};
