import type { Contract } from './index.js';
import type { AbiParamsCommon, ContractAbiInterface, FunctionFragment, GetMethodsTypeFromAbi, GetOutputsType, GetParamsType, IsConstAbi } from '../../types/ABI.js';
import { IsNever, Prettify, UnionToIntersection } from '../../types/UtilsTypes.js';
type IsReadOnlyFunction<Fragment> = Fragment extends FunctionFragment ? Fragment['stateMutability'] extends 'view' | 'pure' ? true : Fragment['constant'] extends true ? true : false : false;
type IsWriteFunction<Fragment> = Fragment extends FunctionFragment ? IsReadOnlyFunction<Fragment> extends true ? false : true : false;
type ReadContractFragments<Abi extends ContractAbiInterface> = Abi[number] extends infer Fragment ? Fragment extends FunctionFragment ? IsReadOnlyFunction<Fragment> extends true ? Fragment : never : never : never;
type WriteContractFragments<Abi extends ContractAbiInterface> = Abi[number] extends infer Fragment ? Fragment extends FunctionFragment ? IsWriteFunction<Fragment> extends true ? Fragment : never : never : never;
export type ReadContractFunctionName<Abi extends ContractAbiInterface> = ReadContractFragments<Abi>['name'];
export type WriteContractFunctionName<Abi extends ContractAbiInterface> = WriteContractFragments<Abi>['name'];
export type CollapseSingleItemTuple<Value> = Value extends readonly [infer Only] ? Only : Value;
type ChangeNeverToString<T> = IsNever<T> extends true ? string : T;
export type ReadContractParameters<AbiFrag extends FunctionFragment = FunctionFragment> = {
    readonly functionName: ChangeNeverToString<AbiFrag['name']>;
    readonly args?: GetParamsType<AbiFrag['inputs']>;
    /** Caller address for the constant call. */
    readonly from?: string;
    readonly value?: number | bigint;
};
export type ReadContractReturnType<AbiFrag extends FunctionFragment = FunctionFragment> = CollapseSingleItemTuple<GetOutputsType<AbiFrag['outputs']>>;
export type WriteContractParameters<AbiFrag extends FunctionFragment = FunctionFragment> = {
    readonly functionName: ChangeNeverToString<AbiFrag['name']>;
    readonly args?: GetParamsType<AbiFrag['inputs']>;
    /**
     * Signer: a private key whose derived address owns and signs the transaction,
     * allowing a non-default signer. When omitted, the instance default key signs.
     */
    readonly account?: string;
    readonly value?: number | bigint;
    readonly feeLimit?: number;
    readonly tokenId?: string;
    readonly tokenValue?: number;
    readonly permissionId?: number;
};
export type WriteContractReturnType = string;
export type ReadOptions<AbiFrag extends FunctionFragment> = Prettify<Omit<ReadContractParameters<AbiFrag>, 'functionName' | 'args'>>;
export type WriteOptions<AbiFrag extends FunctionFragment> = Prettify<Omit<WriteContractParameters<AbiFrag>, 'functionName' | 'args'>>;
type ReadFragmentSignature<AbiFrag extends FunctionFragment, Parameters = NonNullable<ReadContractParameters<AbiFrag>['args']>, Options = ReadOptions<AbiFrag>> = AbiFrag extends FunctionFragment ? (...parameters: Parameters extends readonly [] ? [
    options?: Options
] : [
    args: Parameters,
    options?: Options
]) => Promise<ReadContractReturnType<AbiFrag>> : never;
type ReadSignaturesForName<Abi extends ContractAbiInterface, Name extends ReadContractFunctionName<Abi>> = Extract<ReadContractFragments<Abi>, {
    name: Name;
}> extends infer F ? F extends FunctionFragment ? ReadFragmentSignature<F> : never : never;
type CanonicalizeIntType<T extends string> = T extends `uint[${infer Rest}` ? `uint256[${Rest}` : T extends `int[${infer Rest}` ? `int256[${Rest}` : T extends 'uint' ? 'uint256' : T extends 'int' ? 'int256' : T;
type ParamTypeSignature<Param extends AbiParamsCommon> = Param['type'] extends `tuple${infer Suffix}` ? Param extends {
    components: infer Components;
} ? Components extends ReadonlyArray<AbiParamsCommon> ? `(${JoinParamTypeSignatures<Components>})${Suffix}` : Param['type'] : Param['type'] : CanonicalizeIntType<Param['type']>;
type JoinParamTypeSignatures<Params extends ReadonlyArray<AbiParamsCommon>> = Params extends readonly [infer Head, ...infer Rest] ? Head extends AbiParamsCommon ? Rest extends ReadonlyArray<AbiParamsCommon> ? Rest extends readonly [] ? ParamTypeSignature<Head> : `${ParamTypeSignature<Head>},${JoinParamTypeSignatures<Rest>}` : ParamTypeSignature<Head> : '' : '';
type FunctionSelector<Fragment extends FunctionFragment> = `${Fragment['name']}(${JoinParamTypeSignatures<NonNullable<Fragment['inputs']>>})`;
type ExtractReadNamespace<Abi extends ContractAbiInterface> = {
    [K in ReadContractFragments<Abi> as K['name']]: UnionToIntersection<ReadSignaturesForName<Abi, K['name']>>;
} & {
    [K in ReadContractFragments<Abi> as FunctionSelector<K>]: ReadFragmentSignature<K>;
};
export type ContractReadNamespace<Abi extends ContractAbiInterface> = IsConstAbi<Abi> extends true ? 'read' extends ReadContractFunctionName<Abi> | WriteContractFunctionName<Abi> ? GetMethodsTypeFromAbi<Abi>['read']['onMethod'] & Prettify<ExtractReadNamespace<Abi>> : Prettify<ExtractReadNamespace<Abi>> : any;
type WriteFragmentSignature<AbiFrag extends FunctionFragment, Parameters = NonNullable<WriteContractParameters<AbiFrag>['args']>, Options = WriteOptions<AbiFrag>> = AbiFrag extends FunctionFragment ? (...parameters: Parameters extends readonly [] ? [
    options?: Options
] : [
    args: Parameters,
    options?: Options
]) => Promise<WriteContractReturnType> : never;
type WriteSignaturesForName<Abi extends ContractAbiInterface, Name extends WriteContractFunctionName<Abi>> = Extract<WriteContractFragments<Abi>, {
    name: Name;
}> extends infer F ? F extends FunctionFragment ? WriteFragmentSignature<F> : never : never;
type ExtractWriteNamespace<Abi extends ContractAbiInterface> = {
    [K in WriteContractFragments<Abi> as K['name']]: UnionToIntersection<WriteSignaturesForName<Abi, K['name']>>;
} & {
    [K in WriteContractFragments<Abi> as FunctionSelector<K>]: WriteFragmentSignature<K>;
};
export type ContractWriteNamespace<Abi extends ContractAbiInterface> = IsConstAbi<Abi> extends true ? 'write' extends ReadContractFunctionName<Abi> | WriteContractFunctionName<Abi> ? GetMethodsTypeFromAbi<Abi>['write']['onMethod'] & Prettify<ExtractWriteNamespace<Abi>> : Prettify<ExtractWriteNamespace<Abi>> : any;
/**
 * Build the `contract.read` namespace: every `view`/`pure` (or legacy
 * `constant`) ABI function exposed as
 * `read.fn([args], { from, value })`, executed through
 * `triggerConstantContract` and resolved to the decoded result
 * (single outputs are collapsed to the bare value).
 */
export declare function buildReadNamespace<Abi extends ContractAbiInterface>(contract: Contract<Abi>): ContractReadNamespace<Abi>;
/**
 * Build the `contract.write` namespace: every state-changing ABI function
 * exposed as `write.fn([args], { account, value, feeLimit, tokenId, tokenValue, permissionId })`,
 * built with `triggerSmartContract`, signed with the private key given as `account`
 * (or the instance's default private key when none is given), broadcast, and
 * resolved to the transaction ID.
 */
export declare function buildWriteNamespace<Abi extends ContractAbiInterface>(contract: Contract<Abi>): ContractWriteNamespace<Abi>;
export {};
