import { CommonTypeJsonSchema as ExtrasSchemaDefinition, TapConfigJsonSchema as TapConfiguratorConfigDefinition, TapTypeJsonSchema as TapTypesSchemaDefinition, } from '@iotize/tap/config/schema/v1';
import Ajv from 'ajv';
import { TapConfiguratorError } from './errors';
const TSTYPE_CLASS = {
    Uint8Array
};
const ɵ0 = (schema) => {
    const classObject = TSTYPE_CLASS[schema];
    return function (data) {
        return !classObject || data instanceof classObject;
    };
};
const tsTypeKeyword = {
    compile: ɵ0
};
export function getJsonSchemaValidator(schemas) {
    schemas = schemas || [
        TapConfiguratorConfigDefinition,
        ExtrasSchemaDefinition,
        TapTypesSchemaDefinition
    ];
    const ajv = new Ajv({
        schemas
    });
    ajv.addKeyword('tsType', tsTypeKeyword);
    const schemaId = 'http://iotize.com/schemas/v1/tap-config.json'; // TODO config must match schema api file value
    const 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) {
    const validator = getJsonSchemaValidator(schemas);
    const valid = validator(config);
    if (!valid) {
        throw new TapConfiguratorError.InvalidSchemaError(validator);
    }
    return validator;
}
export function parseTapConfiguratorConfig(input) {
    let 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdGUtdGFwLWNvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2NvbmZpZ3VyYXRvci8iLCJzb3Vy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