import { IGenericSection } from "../../../generic/types/section";
import { IZendeskContent } from "../../types";
import { getMethods } from "../methods";

const compose = (...fns: Array<Function>): Function => 
    (message: IZendeskContent): Array<any> => fns.map(fn => fn(message));

const getSectionChildren = (message: IZendeskContent) =>  {
     // eslint-disable-next-line no-prototype-builtins
     const isTextTranslationNeeded = message.type !== 'text' && message.hasOwnProperty('text');
     const runTranslations = isTextTranslationNeeded ? 
        compose(getMethods('text'), getMethods(message.type)) :
        compose(getMethods(message.type));

    return runTranslations(message);
};

export const transformZendeskMessage = (message: IZendeskContent): IGenericSection => ({
    type: 'section',
    props: {},
    children: [...getSectionChildren(message)]
});