type Peerius = {
    sendAjax: (url: string, apiVersion?: string) => void;
    smartRecsClick: (recommendationId: number) => void;
    smartRecsSendClick: (recommendationId: number) => void;
};

type PeeriusCallbacks = {
    track: { site: string };
    smartRecs: (widgets: ProductRecommendationWidgetModel[]) => void;
    info: (jsonData: any[]) => void;
    apiVersion: string;
};

declare global {
    interface Window {
        Peerius: Peerius;
        PeeriusCallbacks: PeeriusCallbacks;
    }
}

export interface ProductRecommendationWidgetModel {
    widget: string;
    alias: string;
    position: string;
    recs: ProductRecommendationWidgetProductModel[];
}

export interface ProductRecommendationWidgetProductModel {
    id: number;
    refCode: string;
}

export interface ProductRecommendationModel {
    type?: string;
    lang: string;
    channel: string;
    user: {};
    recContent: "full" | "refCodeOnly";
    customAttributes: { locationId: string };
    customer: { customerId?: string; segmentIds?: string[] };
}

export interface ProductRecommendationHomeModel extends ProductRecommendationModel {}

export interface ProductRecommendationCategoryModel extends ProductRecommendationModel {
    category: string;
}

export interface ProductRecommendationProductModel extends ProductRecommendationModel {
    product: { refCode: string };
}

export interface ProductRecommendationBasketItemsModel {
    items: any[];
    currency: string;
    basketId: string;
}

export interface ProductRecommendationBasketModel extends ProductRecommendationModel {
    basket: ProductRecommendationBasketItemsModel;
}

export interface ProductRecommendationOrderModel extends ProductRecommendationModel {
    order: {
        orderNo: string;
        items: any[];
        currency: string;
        subtotal: number;
        shipping: number;
        total: number;
    };
}

export interface ProductRecommendationWishlistModel extends ProductRecommendationModel {
    wishList: {
        items: any[];
    };
}

export interface ProductRecommendationCheckoutModel extends ProductRecommendationModel {
    checkout: {
        items: any[];
        currency: string;
        subtotal: number;
        shipping: number;
        total: number;
    };
}

export interface ProductRecommendationSearchResultsModel extends ProductRecommendationModel {
    searchResults: {
        term: string;
        totalNumberOfResults: number;
        pageNumber: number;
        resultsOnPage: number;
        filters: any[];
        topResults: any[];
    };
}

export interface ProductRecommendationBrandModel extends ProductRecommendationModel {
    brand: string;
}

export function sendTracking(json: string) {
    window.Peerius.sendAjax(`rest.pagex?jd=${json}`, window.PeeriusCallbacks.apiVersion);
}

export function sendImpressionTracking(peeriusSiteName: string, recommendationIds: number[]) {
    window.Peerius.sendAjax(`${peeriusSiteName}/rec/shown.pagex?ids=${recommendationIds.join(",")}`);
}

export function sendProductRecommendationTracking(recommendationId: number, isNewPageWasOpen: boolean) {
    if (isNewPageWasOpen) {
        window.Peerius.smartRecsClick(recommendationId);
    } else {
        window.Peerius.smartRecsSendClick(recommendationId);
    }
}
