"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpInc = void 0;
const AbstractOp_1 = require("./AbstractOp");
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
const constants_1 = require("../constants");
/**
 * @category JSON Patch Extended
 */
class OpInc extends AbstractOp_1.AbstractOp {
    constructor(path, inc) {
        super(path);
        this.inc = inc;
    }
    op() {
        return 'inc';
    }
    code() {
        return constants_1.OPCODE.inc;
    }
    apply(doc) {
        const ref = (0, json_pointer_1.find)(doc, this.path);
        const result = this.inc + Number(ref.val);
        if (ref.obj)
            ref.obj[ref.key] = result;
        else
            doc = result;
        return { doc, old: ref.val };
    }
    toJson(parent) {
        const op = {
            op: 'inc',
            path: (0, json_pointer_1.formatJsonPointer)(this.path),
            inc: this.inc,
        };
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'inc' : constants_1.OPCODE.inc;
        return [opcode, this.path, this.inc];
    }
}
exports.OpInc = OpInc;
//# sourceMappingURL=OpInc.js.map