"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPolymeshStateSelectors = exports.polymeshActions = exports.polymeshReducer = exports.polymeshSlice = void 0;
const toolkit_1 = require("@reduxjs/toolkit");
const initialState = {
    sdk: null,
    wallet: null,
};
exports.polymeshSlice = (0, toolkit_1.createSlice)({
    name: 'polymesh',
    initialState,
    reducers: {
        setSdk(state, action) {
            state.sdk = action.payload;
        },
        setWallet(state, action) {
            state.wallet = action.payload;
        },
        setCurrentAccount(state, action) {
            state.currentAccount = action.payload;
        },
        setHasUid(state, action) {
            state.hasUid = action.payload;
        },
        setCurrentIdentity(state, action) {
            state.currentIdentity = action.payload;
        },
        setIsDidValid(state, action) {
            state.isDidValid = action.payload;
        },
        setDidIssuer(state, action) {
            state.didIssuer = action.payload;
        },
        setDidExpiry(state, action) {
            state.didExpiry = action.payload;
        },
        setPermissions(state, action) {
            state.permissions = action.payload;
        },
        setIsItn(state, action) {
            state.inFlight = action.payload;
        },
        setChainProperties(state, action) {
            state.chainProperties = action.payload;
        },
        setAccountBalance(state, action) {
            state.accountBalance = action.payload;
        },
    },
});
exports.polymeshReducer = exports.polymeshSlice.reducer;
exports.polymeshActions = exports.polymeshSlice.actions;
/**
 * Factory function to return state selectors from provided polymesh selector
 * @param polymeshSelector selector function for polymesh state
 */
function getPolymeshStateSelectors(polymeshSelector) {
    const walletSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => {
        if (polymesh.wallet === null)
            throw new Error('Polymesh wallet is not set');
        return polymesh.wallet;
    });
    const sdkSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => {
        if (polymesh.sdk === null)
            throw new Error('Polymesh SDK not set');
        return polymesh.sdk;
    });
    const currentAccountSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.currentAccount);
    const currentIdentitySelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.currentIdentity);
    const isDidValidSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.isDidValid);
    const didIssuerSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.didIssuer);
    const didExpirySelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.didExpiry);
    const hasUidSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.hasUid);
    const permissionsSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.permissions);
    const inFlightSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.inFlight);
    const chainPropertiesSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.chainProperties);
    const accountBalanceSelector = (0, toolkit_1.createSelector)(polymeshSelector, (polymesh) => polymesh.accountBalance);
    const availableBalanceSelector = (0, toolkit_1.createSelector)(accountBalanceSelector, (balance) => balance === null || balance === void 0 ? void 0 : balance.free.minus(balance.locked));
    return {
        polymeshSelector,
        walletSelector,
        sdkSelector,
        currentAccountSelector,
        currentIdentitySelector,
        isDidValidSelector,
        didIssuerSelector,
        didExpirySelector,
        hasUidSelector,
        permissionsSelector,
        inFlightSelector,
        chainPropertiesSelector,
        accountBalanceSelector,
        availableBalanceSelector,
    };
}
exports.getPolymeshStateSelectors = getPolymeshStateSelectors;
//# sourceMappingURL=polymesh.js.map