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


export default class SimpleStrategy implements ActionStrategy {

    static makeAction(rule, event, listenedElement): Action {
        return new Action(rule.prefix);
    }

    static actionToElements(rule: Rule, action: Action): Element[] {
        if (action.prefix !== rule.prefix) {
            return [];
        }
        let elements;
        if (rule.xpath){
            let result = document.evaluate(rule.xpath, document, null, XPathResult.ANY_TYPE, null);
            elements = [];
            let node = result.iterateNext();
            while (node) {
                elements.push(node);
                node = result.iterateNext();
            }            
        } else {
            elements = querySelectorAllDeep(rule.css);
        }
        return elements;
    } 
}
