"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScriptSearch = void 0;
const jsonpointer_1 = __importDefault(require("jsonpointer"));
const util = __importStar(require("../util/index.js"));
class ScriptSearch {
    constructor(script) {
        this.script = script;
    }
    *search(queries, options = {}) {
        const { returnPipes = true, removeDuplicates = true } = options;
        const emittedIds = new Set();
        for (const r of this.searchAll(queries, returnPipes)) {
            if (removeDuplicates) {
                if (emittedIds.has(r.id)) {
                    continue;
                }
                emittedIds.add(r.id);
            }
            yield r;
        }
    }
    *searchAll(queries, returnPipes) {
        const contextQueries = queries.filter(q => q.type === 'context');
        const actionQueries = queries.filter(q => q.type === 'action');
        const pipeQueries = queries.filter(q => q.type === 'pipe');
        for (const context of this.script.contexts) {
            if (this.matches(context, contextQueries)) {
                yield context;
            }
            for (const action of context.descendentActions()) {
                if (this.matches(action, actionQueries)) {
                    yield action;
                    if (!returnPipes) {
                        continue;
                    }
                }
                for (const pipe of action.descendentPipes()) {
                    if (this.matches(pipe, pipeQueries)) {
                        if (returnPipes) {
                            yield pipe;
                        }
                        else {
                            yield action;
                            break;
                        }
                    }
                }
            }
        }
    }
    matches(obj, query) {
        if (Array.isArray(query)) {
            return query.some(q => this.matches(obj, q));
        }
        for (const [key, value] of Object.entries(query.props)) {
            if (!this.matchProp(obj, key, value)) {
                return false;
            }
        }
        return true;
    }
    matchProp(obj, k, v) {
        const actualValue = k.startsWith('/') ? jsonpointer_1.default.get(obj, k) : obj[k];
        switch (util.getType(v)) {
            case 'array':
                return v.some((v) => this.matchProp(obj, k, v));
            case 'string':
            case 'boolean':
            case 'number':
                return util.strEquals(actualValue, v);
            case 'object': {
                const { $regexp, $contains } = v;
                if ($regexp) {
                    return new RegExp($regexp).test(actualValue);
                }
                if ($contains) {
                    return util.strContains(actualValue, $contains);
                }
                return false;
            }
            case 'null':
                return actualValue == null;
            default:
                return false;
        }
    }
}
exports.ScriptSearch = ScriptSearch;
//# sourceMappingURL=search.js.map