"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpTestType = 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 OpTestType extends AbstractPredicateOp_1.AbstractPredicateOp {
    constructor(path, type) {
        super(path);
        this.type = type;
    }
    op() {
        return 'test_type';
    }
    code() {
        return constants_1.OPCODE.test_type;
    }
    test(doc) {
        const { val } = (0, json_pointer_1.find)(doc, this.path);
        if (val === null)
            return this.type.indexOf('null') > -1;
        if (isArray(val))
            return this.type.indexOf('array') > -1;
        if (this.type.indexOf(typeof val) > -1)
            return true;
        if (typeof val === 'number' && val === Math.round(val) && this.type.indexOf('integer') > -1)
            return true;
        return false;
    }
    toJson(parent) {
        const op = {
            op: 'test_type',
            path: (0, json_pointer_1.formatJsonPointer)(parent ? this.path.slice(parent.path.length) : this.path),
            type: this.type,
        };
        return op;
    }
    toCompact(parent, verbose) {
        const opcode = verbose ? 'test_type' : constants_1.OPCODE.test_type;
        return [opcode, parent ? this.path.slice(parent.path.length) : this.path, this.type];
    }
}
exports.OpTestType = OpTestType;
//# sourceMappingURL=OpTestType.js.map