import { AbstractPredicateOp } from './AbstractPredicateOp';
import { find, formatJsonPointer } from '@jsonjoy.com/json-pointer';
import { OPCODE } from '../constants';
/**
 * @category JSON Patch Extended
 */
export class OpTestStringLen extends AbstractPredicateOp {
    len;
    not;
    constructor(path, len, not) {
        super(path);
        this.len = len;
        this.not = not;
    }
    op() {
        return 'test_string_len';
    }
    code() {
        return OPCODE.test_string_len;
    }
    test(doc) {
        const { val } = find(doc, this.path);
        if (typeof val !== 'string')
            return false;
        const length = val.length;
        const test = length >= this.len;
        return this.not ? !test : test;
    }
    toJson(parent) {
        const op = {
            op: 'test_string_len',
            path: formatJsonPointer(parent ? this.path.slice(parent.path.length) : this.path),
            len: this.len,
        };
        if (this.not)
            op.not = this.not;
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'test_string_len' : OPCODE.test_string_len;
        const path = parent ? this.path.slice(parent.path.length) : this.path;
        return this.not ? [opcode, path, this.len, 1] : [opcode, path, this.len];
    }
}
//# sourceMappingURL=OpTestStringLen.js.map