import React from 'react';
import PropTypes from 'prop-types';
import './styles/ProductPlan.scss';
import withListener from '../withListener';
import Bullet from '../../atoms/bullet/Bullet';
import { createNamespace } from '../../tools/namingTools';
import { generateUniqueID } from '../../tools/helpers';
import { editionsPropTypes } from '../../constants/prop-types/application/editions';

const n = createNamespace('ProductPlan');

export const ProductPlanComponent = ({
    data: {
        bullets,
        footnotes,
        i18n
    }
}) => {
    const i18nWithDefaults = { ...ProductPlanComponent.defaultProps.data.i18n, ...i18n };
    const hasBullets = Boolean(bullets && bullets.length);
    const hasFootnotes = Boolean(footnotes && footnotes.length);
    const bulletModifiers = [hasFootnotes && 'with-footnotes'];
    const footnoteModifers = [hasBullets && 'with-bullets'];

    if (!hasBullets && !hasFootnotes) {
        return null;
    }

    const renderBullet = (item, index) => {
        if (!item.content) {
            return null;
        }

        const { content } = item;
        const uid = generateUniqueID([content.substring(0, 4), index]);

        return (
            <li {...n('bullet', bulletModifiers).withTestId().withE2EId().props} key={uid}>
                <Bullet content={content} iconType="check" />
            </li>
        );
    };

    const renderFootnote = (item, index) => {
        const uid = generateUniqueID([item.substring(0, 4), index]);

        return (
            <p {...n('footnote').withTestId().props} key={uid}>{item}</p>
        );
    };

    return (
        <div {...n('container').props}>
            <div {...n('row').props}>
                {hasBullets && (
                    <div {...n('bullets', bulletModifiers).withTestId().withE2EId().props}>
                        {i18nWithDefaults.bulletsHeader && <h2 {...n('title').withE2EId().props}>{i18nWithDefaults.bulletsHeader}</h2>}
                        <ul {...n('bulletList').props} aria-label={i18nWithDefaults.bulletsLabel}>
                            {bullets.map(renderBullet)}
                        </ul>
                    </div>
                )}
                {hasFootnotes && (
                    <div {...n('footnotes', footnoteModifers).withTestId().props}>
                        <h2 {...n('title').props}>{i18nWithDefaults.footnotes}</h2>
                        {footnotes.map(renderFootnote)}
                    </div>
                )}
            </div>
        </div>
    );
};

ProductPlanComponent.schema = () => ({
    name: 'ProductPlan',
    title: 'uieditor.sfbComponent.productPlan.title',
    iconName: '',
    form: {}
});

ProductPlanComponent.propTypes = {
    data: PropTypes.shape(editionsPropTypes)
};

ProductPlanComponent.defaultProps = {
    data: {
        bullets: [],
        footnotes: [],
        i18n: {
            bulletsHeader: 'All Plans Include',
            bulletsLabel: 'Plan\'s Features',
            footnotes: 'Notes'
        }
    }
};

export default withListener(ProductPlanComponent);
