import { E as EventBus } from './p-9d530225.js';

// types here need to be synced with the types in sdk-dapp-core ledger.types.ts
var LedgerConnectEventsEnum;
(function (LedgerConnectEventsEnum) {
    LedgerConnectEventsEnum["CONNECT_DEVICE"] = "CONNECT_DEVICE";
    LedgerConnectEventsEnum["ACCESS_WALLET"] = "ACCESS_WALLET";
    LedgerConnectEventsEnum["NEXT_PAGE"] = "NEXT_PAGE";
    LedgerConnectEventsEnum["PREV_PAGE"] = "PREV_PAGE";
    LedgerConnectEventsEnum["CLOSE"] = "CLOSE";
    LedgerConnectEventsEnum["DATA_UPDATE"] = "DATA_UPDATE";
})(LedgerConnectEventsEnum || (LedgerConnectEventsEnum = {}));

const getLedgerAddressByIndex = ({ accounts, selectedIndex }) => {
    var _a;
    return ((_a = accounts === null || accounts === void 0 ? void 0 : accounts.find(({ index }) => index === selectedIndex)) === null || _a === void 0 ? void 0 : _a.address) || '';
};

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();
    }
}

export { LedgerConnectBase as L, LedgerConnectEventsEnum as a };

//# sourceMappingURL=p-0d9caba2.js.map