import filter from 'lodash/filter';
import map from 'lodash/map';
import React, { useContext } from 'react';
import { RulesDispatchContext, RulesStateContext } from '../../context/RulesContext';
import { AppRouteSections } from '../../reducers/RouteReducer';
import { setEARulesVisibilityChange } from '../../reducers/RulesReducer';
import Rule from '../shared/Rule';

interface IProps { }

export default function FileEARule(props: IProps) {
    const { rulesState } = useContext(RulesStateContext);
    const rulesDispatch = useContext(RulesDispatchContext);

    const validAttachmentRules = filter(rulesState.EARules.rules, (r) => (r.visible && r.rule_type === 'enrichment' && r.rule_sub_type === 'attachment'));
    const canShowFileEARule = (): boolean => (rulesState.EARules.sidebarSections.indexOf(AppRouteSections.IMPROVE_CASE) !== -1);
    const onHideCPRule = (cta: string) => setEARulesVisibilityChange(rulesDispatch, { 'cta': cta });

    return (<>{canShowFileEARule &&
        map(validAttachmentRules, rule => <Rule onHide={onHideCPRule.bind(null, rule.cta)}
            description={rule.description}
            title={rule.title}
            link={rule.cta}
            key={rule.cta} />)}
    </>);
}
