import Action from './Action';
import Rule from './Rule';


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

    makeAction(event: any): Action {
        return new Action(this.prefix, event.composedPath()[0].innerText);
    }
    
    actionToElements(action: Action): Element[] {
        if (action.prefix !== this.prefix) {
            return [];
        }
        let elements = this.findMatchedElements();
        if (action.suffix) {
            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
        } else {
            return elements;
        }
    }
}