"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const loggingService_1 = require("./loggingService");
const pubSubEventService_1 = require("./pubSubEventService");
const utils_1 = require("./utils");
class AccountService {
    /**
     * Initializes the system that watches for default account changes.
     *
     * `initiateAccountWatch` is called automatically by Arc.js when you pass `true`
     * for `watchForAccountChanges` to `InitializeArcJs`.  You may also call it manually yourself.
     *
     * Then you may request to be notified whenever the current account changes by calling
     * [AccountService.subscribeToAccountChanges](/api/classes/AccountService#subscribeToAccountChanges)
     *
     *
     * @param web3
     */
    static initiateAccountWatch() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (AccountService.accountChangedTimerId) {
                return;
            }
            loggingService_1.LoggingService.info("Initiating account watch");
            if (!AccountService.currentAccount) {
                try {
                    AccountService.currentAccount = yield utils_1.Utils.getDefaultAccount();
                }
                catch (_a) {
                    AccountService.currentAccount = undefined;
                }
            }
            AccountService.accountChangedTimerId = setInterval(() => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (AccountService.accountChangedLock) {
                    return; // prevent reentrance
                }
                AccountService.accountChangedLock = true;
                let currentAccount = AccountService.currentAccount;
                try {
                    currentAccount = yield utils_1.Utils.getDefaultAccount();
                }
                catch (_b) {
                    currentAccount = undefined;
                }
                if (currentAccount !== AccountService.currentAccount) {
                    AccountService.currentAccount = currentAccount;
                    loggingService_1.LoggingService.info(`Account watch: account changed: ${currentAccount}`);
                    pubSubEventService_1.PubSubEventService.publish(AccountService.AccountChangedEventTopic, currentAccount);
                }
                AccountService.accountChangedLock = false;
            }), 1000);
        });
    }
    /**
     * Turn off the system that watches for default account changes.
     */
    static endAccountWatch() {
        if (AccountService.accountChangedTimerId) {
            clearInterval(AccountService.accountChangedTimerId);
            AccountService.accountChangedTimerId = undefined;
        }
    }
    /**
     * Subscribe to be notified whenever the current account changes, like this:
     * ```typescript
     * AccountService.subscribeToAccountChanges((account: Address) => { ... });`
     * ```
     * @param callback
     * @returns A subscription to the event.  Unsubscribe by calling `[theSubscription].unsubscribe()`.
     */
    static subscribeToAccountChanges(callback) {
        return pubSubEventService_1.PubSubEventService.subscribe(AccountService.AccountChangedEventTopic, (topic, address) => callback(address));
    }
}
AccountService.AccountChangedEventTopic = "AccountService.account.changed";
AccountService.accountChangedLock = false;
exports.AccountService = AccountService;
//# sourceMappingURL=accountService.js.map