import React from 'react';
import PropTypes from 'prop-types';
import withListener from '../withListener';
import { filterBundleActions } from '../../tools/callToActions';
import { bundleBannerFormSchema } from '../../constants/components/schemas/product-banner';
import { ProductBannerComponent } from './ProductBanner';

export const BundleBannerComponent = props => {
    // transform Bundle summary to default summary schema
    const { data = {} } = props;
    const { summary, callToActions } = data;
    const resolvedBundleUuid = data?.bundleUuid
        || data?.uuid
        || data?.leads?.form?.otherData?.profileProduct?.id;
    const bundleSummary = summary && {
        title: summary.title,
        details: summary.description,
        logoSrc: summary.images && summary.images[0]
    };
    const bundleCallToActions = callToActions && {
        ...callToActions,
        items: callToActions.items && callToActions.items.length ? callToActions.items.filter(filterBundleActions) : []
    };
    const transformedProps = {
        ...props,
        data: {
            ...data,
            bundleUuid: resolvedBundleUuid,
            summary: bundleSummary,
            callToActions: bundleCallToActions,
            isBundleBanner: true
        }
    };

    return <ProductBannerComponent {...transformedProps} />;
};

BundleBannerComponent.schema = () => ({
    name: 'BundleBanner',
    title: 'uieditor.sfbComponent.bundleBanner.title',
    iconName: 'banner',
    form: bundleBannerFormSchema,
    marketplaceData: {
        applicationType: 'application.config.type',
        categories: 'application.categories',
        configurator: 'application.configurator',
        i18n: 'i18n',
        config: 'config',
        listingPageUrl: 'config.listingEndpoint',
        relatedContent: 'application.relatedContent',
        defaultSelectedEdition: 'application.defaultSelectedEdition',
        locale: 'config.locale.code',
        editions: 'application.editions',
        addonProducts: 'application.addonProducts',
        summary: 'bundle.summary',
        callToActions: 'bundle.callToActions',
        bundleUuid: 'bundle.leads.form.otherData.profileProduct.id'
    }
});

BundleBannerComponent.propTypes = {
    data: PropTypes.shape({
        summary: PropTypes.shape({
            title: PropTypes.string,
            description: PropTypes.string,
            images: PropTypes.arrayOf(PropTypes.string)
        })
    })
};

BundleBannerComponent.defaultProps = {
    data: {}
};

export default withListener(BundleBannerComponent);
