var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { CodeError } from '@iotize/common/error';
import { FirmwareState } from '@iotize/tap/service/impl/firmware';
/**
 * TODO move to TapConfiguratorError
 */
var ConfigKeyNotSupportedByFirmwareVersion = /** @class */ (function (_super) {
    __extends(ConfigKeyNotSupportedByFirmwareVersion, _super);
    function ConfigKeyNotSupportedByFirmwareVersion(configKey, firmwareVersion, constraint) {
        var _this = _super.call(this, "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;
        _this.configKey = configKey;
        _this.firmwareVersion = firmwareVersion;
        _this.constraint = constraint;
        return _this;
    }
    return ConfigKeyNotSupportedByFirmwareVersion;
}(Error));
export { ConfigKeyNotSupportedByFirmwareVersion };
var TapConfiguratorError = /** @class */ (function (_super) {
    __extends(TapConfiguratorError, _super);
    function TapConfiguratorError(code, msg, cause) {
        var _this = _super.call(this, msg, code) || this;
        _this.cause = cause;
        return _this;
    }
    TapConfiguratorError.internalError = function (msg, cause) {
        return new TapConfiguratorError(TapConfiguratorError.Code.InternalError, msg, cause);
    };
    TapConfiguratorError.unexpectedFirmwareState = function (state) {
        return new TapConfiguratorError(TapConfiguratorError.Code.UnexpectedFirmwareState, "Firmware state is not in expected mode. It should be " + FirmwareState.NORMAL + " but found " + state);
    };
    TapConfiguratorError.invalidConfiguration = function (message) {
        return new TapConfiguratorError(TapConfiguratorError.Code.InvalidSchema, message);
    };
    return TapConfiguratorError;
}(CodeError));
export { TapConfiguratorError };
(function (TapConfiguratorError) {
    var Code;
    (function (Code) {
        Code["UnexpectedFirmwareState"] = "TapConfiguratorErrorUnexpectedFirmwareState";
        Code["InvalidSchema"] = "TapConfiguratorErrorIvalidSchema";
        Code["InternalError"] = "TapConfiguratorErrorInternalError";
    })(Code = TapConfiguratorError.Code || (TapConfiguratorError.Code = {}));
    var InvalidSchemaError = /** @class */ (function (_super) {
        __extends(InvalidSchemaError, _super);
        function InvalidSchemaError(validate) {
            var _this = _super.call(this, Code.InvalidSchema, "Invalid Tap configuration file:\n" + InvalidSchemaError.ajvErrorsToString(validate.errors)) || this;
            _this.validate = validate;
            _this.errors = _this.validate.errors || [];
            return _this;
        }
        InvalidSchemaError.ajvErrorsToString = function (errors) {
            // console.log('errors', JSON.stringify(errors, undefined, 4));
            return errors
                ? errors
                    .map(function (error) {
                    var message;
                    switch (error.keyword) {
                        case 'additionalProperties':
                            var 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';
        };
        return InvalidSchemaError;
    }(TapConfiguratorError));
    TapConfiguratorError.InvalidSchemaError = InvalidSchemaError;
})(TapConfiguratorError || (TapConfiguratorError = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXJyb3JzLmpzIiwic291cmNlUm9vdCI6Im5nOi8vQGlvdGl6ZS90YXAvY29uZmlndXJhdG9yLyIsInNvdXJjZXMiOlsibGliL2Vycm9ycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7O0FBQUEsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBQ2pELE9BQU8sRUFBRSxhQUFhLEVBQUUsTUFBTSxtQ0FBb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