import { stateKey } from '../../../../app/state-key.js';
import { productClick } from '../../../../features/commerce/product/product-actions.js';
import { buildInteractiveResultCore, } from '../../../core/interactive-result/headless-core-interactive-result.js';
/**
 * Creates an `InteractiveProduct` sub-controller instance.
 *
 * @param engine - The headless commerce engine.
 * @param props - The configurable `InteractiveProduct` properties.
 * @returns An `InteractiveProduct` sub-controller instance.
 *
 * @group Buildable controllers
 * @category CoreInteractiveProduct
 */
export function buildCoreInteractiveProduct(engine, props) {
    let wasOpened = false;
    const getWarningMessage = () => {
        const messageSegment = (property, lookupFields, fallback) => `- Could not retrieve '${property}' analytics property from field${lookupFields.length > 1 ? 's' : ''} \
'${lookupFields.join("', '")}'; fell back to ${fallback}.`;
        const warnings = [];
        const { ec_name, ec_promo_price, ec_price, ec_product_id } = props.options.product;
        if (!ec_name) {
            warnings.push(messageSegment('name', ['ec_gender'], 'permanentid'));
        }
        if (!ec_promo_price && !ec_price) {
            warnings.push(messageSegment('price', ['ec_promo_price', 'ec_price'], 'NaN'));
        }
        if (!ec_product_id) {
            warnings.push(messageSegment('productId', ['ec_product_id'], 'permanentid'));
        }
        if (warnings.length === 0) {
            return;
        }
        return `Some required analytics properties could not be retrieved from the expected fields for product with \
permanentid '${props.options.product.permanentid}':\n\n${warnings.join('\n')}\n\nReview the configuration of the above \
'ec_'-prefixed fields in your index, and make sure they contain the correct metadata.`;
    };
    const logAnalyticsIfNeverOpened = () => {
        if (wasOpened) {
            return;
        }
        wasOpened = true;
        engine.dispatch(productClick({
            product: {
                name: props.options.product.ec_name ?? props.options.product.permanentid,
                price: props.options.product.ec_promo_price ??
                    props.options.product.ec_price ??
                    NaN,
                productId: props.options.product.ec_product_id ??
                    props.options.product.permanentid,
            },
            position: props.options.product.position,
            responseId: props.responseIdSelector(engine[stateKey]),
        }));
    };
    return {
        ...buildInteractiveResultCore(engine, props, logAnalyticsIfNeverOpened),
        warningMessage: getWarningMessage(),
    };
}
