import { associatedTokenProgram } from '../program';
import { amount, Pda, token } from '../../../types';
import { assert } from '../../../utils';
/** @group Model Helpers */
export const isToken = (value) => typeof value === 'object' && value.model === 'token';
/** @group Model Helpers */
export function assertToken(value) {
    assert(isToken(value), `Expected Token model`);
}
/** @group Model Helpers */
export const toToken = (account) => {
    const associatedTokenAddress = Pda.find(associatedTokenProgram.address, [
        account.data.owner.toBuffer(),
        account.owner.toBuffer(),
        account.data.mint.toBuffer(),
    ]);
    const isAssociatedToken = associatedTokenAddress.equals(account.publicKey);
    return {
        model: 'token',
        address: isAssociatedToken ? associatedTokenAddress : account.publicKey,
        isAssociatedToken,
        mintAddress: account.data.mint,
        ownerAddress: account.data.owner,
        amount: token(account.data.amount.toString()),
        closeAuthorityAddress: account.data.closeAuthorityOption
            ? account.data.closeAuthority
            : null,
        delegateAddress: account.data.delegateOption ? account.data.delegate : null,
        delegateAmount: token(account.data.delegatedAmount.toString()),
        state: account.data.state,
    };
};
/** @group Model Helpers */
export const isTokenWithMint = (value) => typeof value === 'object' && value.model === 'tokenWithMint';
/** @group Model Helpers */
export function assertTokenWithMint(value) {
    assert(isTokenWithMint(value), `Expected TokenWithMint model`);
}
/** @group Model Helpers */
export const toTokenWithMint = (tokenAccount, mintModel) => {
    const token = toToken(tokenAccount);
    return Object.assign(Object.assign({}, token), { model: 'tokenWithMint', mint: mintModel, amount: amount(token.amount.basisPoints, mintModel.currency), delegateAmount: amount(token.delegateAmount.basisPoints, mintModel.currency) });
};
//# sourceMappingURL=Token.js.map