import { SafeDictionary } from "@insite/client-framework/Common/Types";
import getJsonLdScriptId from "@insite/client-framework/Common/Utilities/getJsonLdScriptId";
import setPageMetadata, { Metadata } from "@insite/client-framework/Common/Utilities/setPageMetadata";
import { createHandlerChainRunnerOptionalParameter, Handler } from "@insite/client-framework/HandlerCreator";
import { Website } from "@insite/client-framework/Services/WebsiteService";
import { getSettingsCollection } from "@insite/client-framework/Store/Context/ContextSelectors";
import {
    getAlternateLanguageUrls,
    getCanonicalUrlByPath,
    getCurrentPage,
} from "@insite/client-framework/Store/Data/Pages/PageSelectors";
import { WebsiteSettingsModel } from "@insite/client-framework/Types/ApiModels";

type HandlerType = Handler<{}, { metadata?: Metadata; websiteSettings?: WebsiteSettingsModel; website?: Website }>;

export const DispatchCalledSetMetadata: HandlerType = ({ dispatch }) => {
    // we use this to determine if we need to call this handler during SSR
    dispatch({ type: "Context/CalledSetMetadata" });
};

export const ShouldSetMetadata: HandlerType = ({ getState, dispatch }) => {
    const state = getState();
    const page = getCurrentPage(state);

    if (
        !page ||
        page.type === "ProductListPage" ||
        page.type === "ProductDetailsPage" ||
        page.type === "CategoryDetailsPage"
    ) {
        return false;
    }
};

export const LookUpData: HandlerType = props => {
    const state = props.getState();
    const page = getCurrentPage(state);

    const pathname = state.data.pages.location.pathname;
    const canonicalUrl = getCanonicalUrlByPath(state, pathname) || pathname;
    const alternateLanguageUrls = getAlternateLanguageUrls(state, page.id);
    const websiteName = state.context.website.name;

    props.metadata = {
        metaKeywords: page.fields.metaKeywords,
        metaDescription: page.fields.metaDescription,
        openGraphUrl: page.fields.openGraphUrl,
        openGraphTitle: page.fields.openGraphTitle,
        openGraphImage: page.fields.openGraphImage,
        structuredPageData: page.fields.structuredPageData,
        title: page.fields.title,
        currentPath: pathname,
        canonicalPath: canonicalUrl,
        alternateLanguageUrls,
        websiteName,
        jsonLdScriptId: getJsonLdScriptId(page.type),
        pageType: page.type,
        hideFromSearchEngines: page.fields.hideFromSearchEngines,
        doNotFollowLinks: page.fields.doNotFollowLinks,
    };

    props.websiteSettings = getSettingsCollection(state).websiteSettings;

    props.website = state.context.website;
};

export const SetMetadata: HandlerType = ({ metadata, websiteSettings, website }) => {
    if (metadata) {
        setPageMetadata(metadata, websiteSettings, website);
    }
};

export const chain = [DispatchCalledSetMetadata, ShouldSetMetadata, LookUpData, SetMetadata];

export const setMetadata = createHandlerChainRunnerOptionalParameter(chain, {}, "SetMetaData");
