import { querySelectorAllDeep } from 'query-selector-shadow-dom';
import ActionStrategy from './ActionStrategy';
import Action from '../../Action';
import Rule from '../Rule';


export default class InnerTextStrategy implements ActionStrategy {

    static makeAction(rule: any, event: any, listenedElement: any): Action {
        return new Action(rule.prefix, listenedElement.innerText);
    }
    
    static actionToElements(rule: Rule, action: Action): Element[] {
        if (action.prefix !== rule.prefix) {
            return [];
        }
        let domElements = querySelectorAllDeep(rule.selector);
        if (action.suffix) {
            return domElements.filter(domElement => 
                domElement.innerText.trim() === action.suffix.trim()
            )
        //Happens if a previous rules was not using the innerText suffix
        } else {
            return domElements;
        }
    }
}