"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpTestStringLen = void 0;
const AbstractPredicateOp_1 = require("./AbstractPredicateOp");
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
const constants_1 = require("../constants");
/**
 * @category JSON Patch Extended
 */
class OpTestStringLen extends AbstractPredicateOp_1.AbstractPredicateOp {
    constructor(path, len, not) {
        super(path);
        this.len = len;
        this.not = not;
    }
    op() {
        return 'test_string_len';
    }
    code() {
        return constants_1.OPCODE.test_string_len;
    }
    test(doc) {
        const { val } = (0, json_pointer_1.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: (0, json_pointer_1.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' : constants_1.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];
    }
}
exports.OpTestStringLen = OpTestStringLen;
//# sourceMappingURL=OpTestStringLen.js.map