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