import StyledWrapper from "@insite/client-framework/Common/StyledWrapper";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getWishListState } from "@insite/client-framework/Store/Data/WishLists/WishListsSelectors";
import translate from "@insite/client-framework/Translate";
import WidgetModule from "@insite/client-framework/Types/WidgetModule";
import WidgetProps from "@insite/client-framework/Types/WidgetProps";
import StaticListProductListLine from "@insite/content-library/Widgets/StaticList/StaticListProductListLine";
import Typography, { TypographyPresentationProps } from "@insite/mobius/Typography";
import getColor from "@insite/mobius/utilities/getColor";
import InjectableCss from "@insite/mobius/utilities/InjectableCss";
import React from "react";
import { connect } from "react-redux";
import { css } from "styled-components";

const mapStateToProps = (state: ApplicationState) => ({
    wishListState: getWishListState(state, state.pages.staticList.wishListId),
    productInfosByWishListLineId: state.pages.staticList.productInfosByWishListLineId,
});

type Props = WidgetProps & ReturnType<typeof mapStateToProps>;

export interface StaticListProductListStyles {
    mainWrapper?: InjectableCss;
    countText?: TypographyPresentationProps;
    linesWrapper?: InjectableCss;
}

export const staticListProductListStyles: StaticListProductListStyles = {
    mainWrapper: {
        css: css`
            margin-top: 10px;
        `,
    },
    countText: {
        weight: 600,
    },
    linesWrapper: {
        css: css`
            margin-top: 10px;
            border-top: 1px solid ${getColor("common.border")};
        `,
    },
};

const styles = staticListProductListStyles;

const StaticListProductList = ({ wishListState, productInfosByWishListLineId }: Props) => {
    if (wishListState.isLoading || !wishListState.value || Object.keys(productInfosByWishListLineId).length === 0) {
        return null;
    }

    const wishList = wishListState.value;

    return (
        <StyledWrapper {...styles.mainWrapper}>
            <Typography {...styles.countText}>
                {wishList.wishListLinesCount === 1 && translate("{0} Product", wishList.wishListLinesCount.toString())}
                {wishList.wishListLinesCount > 1 && translate("{0} Products", wishList.wishListLinesCount.toString())}
            </Typography>
            <StyledWrapper {...styles.linesWrapper}>
                {wishList.wishListLineCollection?.map(wishListLine => {
                    const productInfo = productInfosByWishListLineId[wishListLine.id];
                    if (!productInfo) {
                        return null;
                    }

                    return (
                        <StaticListProductListLine
                            key={wishListLine.id}
                            wishListLine={wishListLine}
                            productInfo={productInfo}
                        />
                    );
                })}
            </StyledWrapper>
        </StyledWrapper>
    );
};

const widgetModule: WidgetModule = {
    component: connect(mapStateToProps)(StaticListProductList),
    definition: {
        group: "Static List",
        displayName: "Product List",
        allowedContexts: ["StaticListPage"],
    },
};

export default widgetModule;
