import { CodeError } from '@iotize/common/error';
import { FirmwareState } from '@iotize/tap/service/impl/firmware';
/**
 * TODO move to TapConfiguratorError
 */
export class ConfigKeyNotSupportedByFirmwareVersion extends Error {
    constructor(configKey, firmwareVersion, constraint) {
        super(`Configuration key "${configKey}" is not available for you firmware version (${firmwareVersion}). \
${constraint.minApiVersion
            ? `Minimal firmware version: ${constraint.minApiVersion}.`
            : ''}\
${constraint.maxApiVersion
            ? `Maximal firmware version: ${constraint.maxApiVersion}.`
            : ''}`);
        this.configKey = configKey;
        this.firmwareVersion = firmwareVersion;
        this.constraint = constraint;
    }
}
export class TapConfiguratorError extends CodeError {
    constructor(code, msg, cause) {
        super(msg, code);
        this.cause = cause;
    }
    static internalError(msg, cause) {
        return new TapConfiguratorError(TapConfiguratorError.Code.InternalError, msg, cause);
    }
    static unexpectedFirmwareState(state) {
        return new TapConfiguratorError(TapConfiguratorError.Code.UnexpectedFirmwareState, `Firmware state is not in expected mode. It should be ${FirmwareState.NORMAL} but found ${state}`);
    }
    static invalidConfiguration(message) {
        return new TapConfiguratorError(TapConfiguratorError.Code.InvalidSchema, message);
    }
}
(function (TapConfiguratorError) {
    let Code;
    (function (Code) {
        Code["UnexpectedFirmwareState"] = "TapConfiguratorErrorUnexpectedFirmwareState";
        Code["InvalidSchema"] = "TapConfiguratorErrorIvalidSchema";
        Code["InternalError"] = "TapConfiguratorErrorInternalError";
    })(Code = TapConfiguratorError.Code || (TapConfiguratorError.Code = {}));
    class InvalidSchemaError extends TapConfiguratorError {
        constructor(validate) {
            super(Code.InvalidSchema, `\
Invalid Tap configuration file:
${InvalidSchemaError.ajvErrorsToString(validate.errors)}`);
            this.validate = validate;
            this.errors = this.validate.errors || [];
        }
        static ajvErrorsToString(errors) {
            // console.log('errors', JSON.stringify(errors, undefined, 4));
            return errors
                ? errors
                    .map((error) => {
                    let message;
                    switch (error.keyword) {
                        case 'additionalProperties':
                            const params = error.params;
                            message = `${error.dataPath} should not have additional property "${params.additionalProperty}"`;
                            break;
                        default:
                            message = `${error.dataPath} ${error.message} (keyword: ${error.keyword})`;
                    }
                    return `\t- ${message}`;
                })
                    .join('\n')
                : 'Unknown errors';
        }
    }
    TapConfiguratorError.InvalidSchemaError = InvalidSchemaError;
})(TapConfiguratorError || (TapConfiguratorError = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXJyb3JzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vY29uZmlndXJhdG9yL3NyYy9saWIvZXJyb3JzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxTQUFTLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUNqRCxPQUFPLEVBQUUsYUFBYSxFQUFFLE1BQU0sbUNBQW1DLENBQUM7QUFHbEU7O0dBRUc7QUFDSCxNQUFNLE9BQU8sc0NBQXVDLFNBQVEsS0FBSztJQUMvRCxZQUNrQixTQUFpQixFQUNqQixlQUF1QixFQUN2QixVQUdmO1FBRUQsS0FBSyxDQUFDLHNCQUFzQixTQUFTLGdEQUFnRCxlQUFlO0VBRXRHLFVBQVUsQ0FBQyxhQUFhO1lBQ3RCLENBQUMsQ0FBQyw2QkFBNkIsVUFBVSxDQUFDLGFBQWEsR0FBRztZQUMxRCxDQUFDLENBQUMsRUFDTjtFQUVFLFVBQVUsQ0FBQyxhQUFhO1lBQ3RCLENBQUMsQ0FBQyw2QkFBNkIsVUFBVSxDQUFDLGFBQWEsR0FBRztZQUMxRCxDQUFDLENBQUMsRUFDTixFQUFFLENBQUMsQ0FBQztRQWpCZ0IsY0FBUyxHQUFULFNBQVMsQ0FBUTtRQUNqQixvQkFBZSxHQUFmLGVBQWUsQ0FBUTtRQUN2QixlQUFVLEdBQVYsVUFBVSxDQUd6QjtJQWFILENBQUM7Q0FDRjtBQUVELE1BQU0sT0FBTyxvQkFBcUIsU0FBUSxTQUFvQ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