import { b as Credentials, c as Error, C as Connection, I as Identifier, T as Token } from './index.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.toBasic(credentials),
            });
            if (!Error.is(result)) {
                Connection.key = Array.isArray(result.merchant) ? result.merchant[0].key.private : result.merchant.key.private;
                this.user = result;
            }
            return result;
        });
    }
    logout() {
        Connection.url = undefined;
        Connection.key = undefined;
        this.user = undefined;
    }
    get user() {
        const storage = 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 = 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 = 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 (Identifier.is(item, 16))
                result.push(storage === null || storage === void 0 ? void 0 : storage.getItem(item));
        }
        return result.filter(Token.is);
    }
    addCustomerKey(key, customerId) {
        const storage = Connection.storage;
        storage && storage.setItem(customerId, key);
    }
    static open(url, key) {
        const connection = Connection.open(url, key);
        return connection && new Me(connection);
    }
}

export { Me };
