'use strict';

const Credentials = require('./Credentials-8e4749ab.js');
const Token = require('./Token-8c5ba5d9.js');
const Item = require('./Item-00ce8c24.js');
require('./TextDecoder-753156f3.js');
require('./_commonjsHelpers-537d719a.js');
require('./index-09251373.js');

var __awaiter = (undefined && undefined.__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());
    });
};
class Me {
    constructor(connection) {
        this.connection = connection;
        this.connection.onUnauthorized = () => __awaiter(this, void 0, void 0, function* () { return this.onUnauthorized != undefined && (yield this.onUnauthorized(this)); });
    }
    login(user, password) {
        return __awaiter(this, void 0, void 0, function* () {
            const credentials = typeof user == "string" ? { user, password } : user;
            const result = yield this.connection.get("me", {
                authorization: Credentials.Credentials.toBasic(credentials),
            });
            if (!Credentials.Error.is(result)) {
                Item.Connection.key = Array.isArray(result.merchant) ? result.merchant[0].key.private : result.merchant.key.private;
                this.user = result;
            }
            return result;
        });
    }
    logout() {
        Item.Connection.url = undefined;
        Item.Connection.key = undefined;
        this.user = undefined;
    }
    get user() {
        const storage = Item.Connection.storage;
        if (storage) {
            const userValue = storage.getItem("Intergiro user");
            this.userValue = userValue ? JSON.parse(userValue) : undefined;
        }
        return this.userValue;
    }
    set user(value) {
        const storage = Item.Connection.storage;
        if (storage)
            value ? storage.setItem("Intergiro user", JSON.stringify(value)) : storage.removeItem("Intergiro user");
        this.userValue = value;
    }
    get customerKeys() {
        var _a;
        const storage = Item.Connection.storage;
        const result = [];
        for (let i = 0; i < ((_a = storage === null || storage === void 0 ? void 0 : storage.length) !== null && _a !== void 0 ? _a : 0); i++) {
            const item = storage === null || storage === void 0 ? void 0 : storage.key(i);
            if (Token.Identifier.is(item, 16))
                result.push(storage === null || storage === void 0 ? void 0 : storage.getItem(item));
        }
        return result.filter(Token.Token.is);
    }
    addCustomerKey(key, customerId) {
        const storage = Item.Connection.storage;
        storage && storage.setItem(customerId, key);
    }
    static open(url, key) {
        const connection = Item.Connection.open(url, key);
        return connection && new Me(connection);
    }
}

exports.Me = Me;
