"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpType = void 0;
const AbstractPredicateOp_1 = require("./AbstractPredicateOp");
const json_pointer_1 = require("@jsonjoy.com/json-pointer");
const constants_1 = require("../constants");
const { isArray } = Array;
/**
 * @category JSON Predicate
 */
class OpType extends AbstractPredicateOp_1.AbstractPredicateOp {
    constructor(path, value) {
        super(path);
        this.value = value;
    }
    op() {
        return 'type';
    }
    code() {
        return constants_1.OPCODE.type;
    }
    test(doc) {
        const { val } = (0, json_pointer_1.find)(doc, this.path);
        if (val === null)
            return this.value === 'null';
        if (isArray(val))
            return this.value === 'array';
        if (typeof val === this.value)
            return true;
        if (typeof val === 'number' && val === Math.round(val) && this.value === 'integer')
            return true;
        return false;
    }
    toJson(parent) {
        const op = {
            op: 'type',
            path: (0, json_pointer_1.formatJsonPointer)(parent ? this.path.slice(parent.path.length) : this.path),
            value: this.value,
        };
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'type' : constants_1.OPCODE.type;
        return [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.value];
    }
}
exports.OpType = OpType;
//# sourceMappingURL=OpType.js.map