var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import '@iotize/tap/service/impl/group';
import '@iotize/tap/service/impl/interface';
import { debug } from './debug';
import { hashLoginPassword } from './utility';
var TAG = 'BasicAuth';
var BasicAuth = /** @class */ (function () {
    function BasicAuth(tap, options) {
        if (options === void 0) { options = {
            hashPassword: false
        }; }
        this.tap = tap;
        this.options = options;
    }
    Object.defineProperty(BasicAuth.prototype, "hashPassword", {
        get: function () {
            var _a;
            return ((_a = this.options) === null || _a === void 0 ? void 0 : _a.hashPassword) || false;
        },
        enumerable: true,
        configurable: true
    });
    /**
     * Tap login
     *
     */
    BasicAuth.prototype.login = function (params) {
        return __awaiter(this, void 0, void 0, function () {
            var username, password, response;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        username = params.username, password = params.password;
                        if (!this.hashPassword) return [3 /*break*/, 2];
                        debug(TAG, "Using hash password login");
                        return [4 /*yield*/, this.tap.service.interface.loginWithHash({
                                username: username,
                                password: hashLoginPassword(password)
                            })];
                    case 1:
                        response = _a.sent();
                        return [3 /*break*/, 4];
                    case 2:
                        debug(TAG, "Using regular login");
                        return [4 /*yield*/, this.tap.service.interface.login({
                                username: username,
                                password: password
                            })];
                    case 3:
                        response = _a.sent();
                        _a.label = 4;
                    case 4:
                        response.successful();
                        return [2 /*return*/, {}];
                }
            });
        });
    };
    BasicAuth.prototype.logout = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.tap.service.interface.logout()];
                    case 1:
                        (_a.sent()).successful();
                        return [2 /*return*/];
                }
            });
        });
    };
    /**
     * Change password for the groupId (If groupId is not specified, it will be changed for the current group id)
     * @param newPassword the new password
     * @param userIdOrName default to the current group id
     * @param userPassword the current user password. if provided it will perform a login before changing password
     */
    BasicAuth.prototype.changePassword = function (newPassword, userId) {
        return __awaiter(this, void 0, void 0, function () {
            var passwordBytes;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!this.hashPassword) return [3 /*break*/, 2];
                        passwordBytes = hashLoginPassword(newPassword);
                        return [4 /*yield*/, this.tap.service.group.changePasswordKey(userId, passwordBytes)];
                    case 1:
                        (_a.sent()).successful();
                        return [3 /*break*/, 4];
                    case 2: return [4 /*yield*/, this.tap.service.group.changePassword(userId, newPassword)];
                    case 3:
                        (_a.sent()).successful();
                        _a.label = 4;
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    return BasicAuth;
}());
export { BasicAuth };
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzaWMtYXV0aC5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2F1dGgvIiwic291cmNlcyI6WyJsaWIvYmFzaWMtYXV0aC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxPQUFPLGdDQUFnQyxDQUFDO0FBQ3hDLE9BQU8sb0NBQW9DLENBQUM7QUFJNUMsT0FBTyxFQUFFLEtBQUssRUFBRSxNQUFNLFNBQVMsQ0FBQztBQUdoQyxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFOUMsSUFBTSxHQUFHLEdBQUcsV0FBVyxDQUFDO0FBRXhCO0lBTUksbUJBQW9CLEdBQVEsRUFBUyxPQUloQztRQUpnQyx3QkFBQSxFQUFBO1lBRzdCLFlBQVksRUFBRSxLQUFLO1NBQ3RCO1FBSmUsUUFBRyxHQUFILEdBQUcsQ0FBSztRQUFTLFlBQU8sR0FBUCxPQUFPLENBSXZDO0lBRUwsQ0FBQztJQVZELHNCQUFZLG1DQUFZO2FBQXhCOztZQUNJLE9BQU8sT0FBQSxJQUFJLENBQUMsT0FBTywwQ0FBRSxZQUFZLEtBQUksS0FBSyxDQUFDO1FBQy9DLENBQUM7OztPQUFBO0lBVUQ7OztPQUdHO0lBQ1UseUJBQU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