/* eslint-disable spire/export-styles */
import { getIsWebCrawler } from "@insite/client-framework/Common/WebCrawler";
import { HasProduct, withProduct } from "@insite/client-framework/Components/ProductContext";
import { ConfigurationType } from "@insite/client-framework/Services/ProductServiceV2";
import ApplicationState from "@insite/client-framework/Store/ApplicationState";
import { getSettingsCollection } from "@insite/client-framework/Store/Context/ContextSelectors";
import ProductAvailability, { ProductAvailabilityStyles } from "@insite/content-library/Components/ProductAvailability";
import React from "react";
import { connect } from "react-redux";

interface OwnProps {
    isProductDetailsPage?: boolean;
    configurationCompleted?: boolean;
    extendedStyles?: ProductAvailabilityStyles;
}

const mapStateToProps = (state: ApplicationState, ownProps: OwnProps) => {
    return {
        includeRealTimeInventoryAndPricingForSeo:
            getSettingsCollection(state).websiteSettings.includeRealTimeInventoryAndPricingForSeo,
    };
};

type Props = ReturnType<typeof mapStateToProps> & OwnProps & HasProduct;

const ProductContextAvailability = ({
    includeRealTimeInventoryAndPricingForSeo,
    productInfo,
    product,
    isProductDetailsPage,
    configurationCompleted,
    extendedStyles,
}: Props) => {
    const { inventory, unitOfMeasure, failedToLoadInventory } = productInfo;

    const availability =
        inventory?.inventoryAvailabilityDtos?.find(
            o => o.unitOfMeasure.toLowerCase() === (unitOfMeasure?.toLowerCase() || ""),
        )?.availability || undefined;

    if (
        (getIsWebCrawler() && !includeRealTimeInventoryAndPricingForSeo) ||
        product.isVariantParent ||
        (!!product.configurationType &&
            product.configurationType !== ConfigurationType.None &&
            product.configurationType !== ConfigurationType.Fixed &&
            !configurationCompleted)
    ) {
        return null;
    }

    return (
        <ProductAvailability
            productId={product.id}
            trackInventory={product.trackInventory}
            availability={availability}
            unitOfMeasure={unitOfMeasure}
            isProductDetailsPage={isProductDetailsPage}
            failedToLoadInventory={failedToLoadInventory}
            extendedStyles={extendedStyles}
        />
    );
};

export default withProduct(connect(mapStateToProps)(ProductContextAvailability));
