import { EventBus } from "../../../utils/EventBus";
import { getLedgerAddressByIndex } from "./helpers/getLedgerAddressByIndex";
import { LedgerConnectEventsEnum } from "./ledger-connect.types";
export class LedgerConnectBase {
    constructor(data) {
        this.data = data;
        this.eventBus = new EventBus();
        this.selectedIndex = 0;
        this.selectedAddress = '';
    }
    getEventBus() {
        return this.eventBus;
    }
    accessWallet() {
        var _a;
        this.eventBus.publish(LedgerConnectEventsEnum.ACCESS_WALLET, {
            addressIndex: this.selectedIndex,
            selectedAddress: this.selectedAddress || getLedgerAddressByIndex({ accounts: (_a = this.data.accountScreenData) === null || _a === void 0 ? void 0 : _a.accounts, selectedIndex: this.selectedIndex }),
        });
    }
    selectAccount(index) {
        var _a;
        this.selectedIndex = index;
        this.selectedAddress = getLedgerAddressByIndex({ accounts: (_a = this.data.accountScreenData) === null || _a === void 0 ? void 0 : _a.accounts, selectedIndex: this.selectedIndex });
    }
    nextPage() {
        this.eventBus.publish(LedgerConnectEventsEnum.NEXT_PAGE);
    }
    prevPage() {
        this.eventBus.publish(LedgerConnectEventsEnum.PREV_PAGE);
    }
    subscribeEventBus({ closeFn, forceUpdateFn }) {
        this.eventBus.subscribe(LedgerConnectEventsEnum.DATA_UPDATE, (payload) => this.dataUpdate({ payload, closeFn, forceUpdateFn }));
    }
    unsubscribeEventBus({ closeFn, forceUpdateFn }) {
        this.eventBus.unsubscribe(LedgerConnectEventsEnum.DATA_UPDATE, (payload) => this.dataUpdate({ payload, closeFn, forceUpdateFn }));
    }
    dataUpdate({ payload, closeFn, forceUpdateFn }) {
        if (payload.shouldClose) {
            return closeFn();
        }
        this.data = Object.assign({}, payload);
        forceUpdateFn();
    }
}
//# sourceMappingURL=LedgerConnectBase.js.map
