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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFsaWRhdGUtdGFwLWNvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL2N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