import { CommonTypeJsonSchema as ExtrasSchemaDefinition, TapConfigJsonSchema as TapConfiguratorConfigDefinition, TapTypeJsonSchema as TapTypesSchemaDefinition, } from '@iotize/tap/config/schema/v1';
import Ajv from 'ajv';
import { TapConfiguratorError } from './errors';
var TSTYPE_CLASS = {
    Uint8Array: Uint8Array
};
var ɵ0 = function (schema) {
    var classObject = TSTYPE_CLASS[schema];
    return function (data) {
        return !classObject || data instanceof classObject;
    };
};
var tsTypeKeyword = {
    compile: ɵ0
};
export function getJsonSchemaValidator(schemas) {
    schemas = schemas || [
        TapConfiguratorConfigDefinition,
        ExtrasSchemaDefinition,
        TapTypesSchemaDefinition
    ];
    var ajv = new Ajv({
        schemas: schemas
    });
    ajv.addKeyword('tsType', tsTypeKeyword);
    var schemaId = 'http://iotize.com/schemas/v1/tap-config.json'; // TODO config must match schema api file value
    var validator = ajv.getSchema(schemaId);
    if (!validator) {
        throw new Error("Cannot create JSON validator function from schema " + schemaId);
    }
    return validator;
}
// export function checkErrors(config: Object) {
//     const validator = getJsonSchemaValidator();
//     validator(config);
//     return validator.errors;
// }
/**
 * Validate schema. Throw error if schema is not valid
 * @throws @{link TapConfiguratorError}
 * @param config the tap config object
 * @param schemas optial schema used for validation. If not provided, default one will be used
 *
 * @returns validator function
 */
export function validateTapConfiguratorConfig(config, schemas) {
    var validator = getJsonSchemaValidator(schemas);
    var valid = validator(config);
    if (!valid) {
        throw new TapConfiguratorError.InvalidSchemaError(validator);
    }
    return validator;
}
export function parseTapConfiguratorConfig(input) {
    var config;
    try {
        config = JSON.parse(input);
    }
    catch (err) {
        throw new Error("Cannot parse Tap configurator config content. " + err.message);
    }
    validateTapConfiguratorConfig(config);
    return config;
}
export { ɵ0 };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdGUtdGFwLWNvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2NvbmZpZ3VyYXRvci8iLCJ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