import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { ProductModel } from "@insite/client-framework/Types/ApiModels";

export function getProductsForProductInfoList(state: ApplicationState, id: string): ProductModel[] | undefined {
    const mapId = state.components.productInfoLists.dataMapping[id];
    const productInfoByProductId =
        state.components.productInfoLists.productInfoListById[mapId || id]?.productInfoByProductId;
    return productInfoByProductId
        ? Object.keys(productInfoByProductId).map(o => state.data.products.byId[o])
        : undefined;
}

export function getProductInfoFromList(state: ApplicationState, id: string, productId: string) {
    const mapId = state.components.productInfoLists.dataMapping[id];
    return state.components.productInfoLists.productInfoListById[mapId || id]?.productInfoByProductId[productId];
}

export function getErrorMessage(state: ApplicationState, id: string): string | undefined {
    const mapId = state.components.productInfoLists.dataMapping[id];
    return state.components.productInfoLists.errorMessageById[mapId || id];
}
