import { AbstractOp } from './AbstractOp';
import { find, formatJsonPointer } from '@jsonjoy.com/json-pointer';
import { OPCODE } from '../constants';
/**
 * @category JSON Patch Extended
 */
export class OpInc extends AbstractOp {
    inc;
    constructor(path, inc) {
        super(path);
        this.inc = inc;
    }
    op() {
        return 'inc';
    }
    code() {
        return OPCODE.inc;
    }
    apply(doc) {
        const ref = 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: formatJsonPointer(this.path),
            inc: this.inc,
        };
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'inc' : OPCODE.inc;
        return [opcode, this.path, this.inc];
    }
}
//# sourceMappingURL=OpInc.js.map