import Action from './Action';
import SimpleRule from './SimpleRule';


export default class InnerTextRule extends SimpleRule {
    constructor(prefix, suffix, event, css, xpath, code, key, contextURL, contextCSS, contextXPath, description) {
        super(prefix, suffix, event, css, xpath, code, key, contextURL, contextCSS, contextXPath, description);
    }

    makeAction(event: any): Action {
        const element = this.findActionMappedTarget(event);
        return new Action(this.prefix, element.innerText.trim());
    }

    actionToElements(action: Action): Element[] {
        if (action.prefix !== this.prefix) {
            return [];
        }
        if (action.suffix === undefined) {
            return super.actionToElements(action);
        } else {
            const elements = this.findMatchedElements();
            return elements.filter(domElement => {
                if (domElement instanceof HTMLElement) {
                    return domElement.innerText.trim() === action.suffix.trim()
                } else {
                    return false;
                }
            });
        // Happens if a previous rules was not using the innerText suffix
        }
    }
}