import { AbstractPredicateOp } from './AbstractPredicateOp';
import { find, formatJsonPointer } from '@jsonjoy.com/json-pointer';
import { OPCODE } from '../constants';
/**
 * @category JSON Predicate
 */
export class OpMatches extends AbstractPredicateOp {
    value;
    ignore_case;
    matcher;
    // tslint:disable-next-line variable-name
    constructor(path, value, ignore_case, createMatcher) {
        super(path);
        this.value = value;
        this.ignore_case = ignore_case;
        this.matcher = createMatcher(value, ignore_case);
    }
    op() {
        return 'matches';
    }
    code() {
        return OPCODE.matches;
    }
    test(doc) {
        const { val } = find(doc, this.path);
        if (typeof val !== 'string')
            return false;
        const test = this.matcher(val);
        return test;
    }
    toJson(parent) {
        const op = {
            op: 'matches',
            path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
            value: this.value,
        };
        if (this.ignore_case)
            op.ignore_case = this.ignore_case;
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'matches' : OPCODE.matches;
        return this.ignore_case
            ? [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value, 1]
            : [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
    }
}
//# sourceMappingURL=OpMatches.js.map