import { Promisify } from '@f2/promise';
import { Object } from '@f2/ts';
import { Completable, CompletableFn } from './complete';
export type TaskOnProgressFn<TProgressMap extends TaskProgressMap = any> = (step: TaskStep<TProgressMap, keyof TProgressMap>) => void;
export type TaskFunction<T = void> = () => T | PromiseLike<T>;
export type TaskProgressMap = Object<any>;
export interface TaskOptions<TProgressMap extends TaskProgressMap = any> {
    /**
     * @default true
     */
    rejectable?: boolean;
    observer?: TaskObserver<TProgressMap>;
}
export type TaskWithoutProgress<T = void> = Omit<Task<T>, 'onProgress'>;
export type TaskInput<T = void> = PromiseLike<T> | TaskFunction<T> | undefined | void;
export declare class Task<T = void, TProgressMap extends TaskProgressMap = any> extends Promisify<T | undefined> {
    private _observer?;
    private _completable;
    onComplete: (cb: CompletableFn<T>) => void;
    static from<T>(p: TaskInput<T>): TaskWithoutProgress<T>;
    static from<T>(p: TaskInput<T>, rejectable: false): TaskWithoutProgress<T | undefined>;
    static from<T, TProgressMap extends TaskProgressMap>(p: TaskInput<T>, observer: TaskObserver<TProgressMap>): Task<T, TProgressMap>;
    static from<T, TProgressMap extends TaskProgressMap>(p: TaskInput<T>, observer: TaskObserver<TProgressMap>, rejectable: boolean): Task<T | undefined, TProgressMap>;
    constructor(input: PromiseLike<T> | TaskFunction<T> | undefined | void, options?: TaskOptions<TProgressMap>);
    onProgress(cb: TaskOnProgressFn<TProgressMap>): void;
}
export interface TaskObserverTask<TArg = any> {
    resolve: (arg?: TArg) => void;
    reject: (err: Error | string) => void;
}
interface TaskStepEvents {
    onResolve?: () => void;
    onReject?: (err: Error | string) => void;
}
export declare class TaskObserver<TProgressMap extends TaskProgressMap> {
    private _onProgressFns;
    private _step;
    progress<Step extends keyof TProgressMap>(step: Step, arg?: TProgressMap[Step]): void;
    task<Step extends keyof TProgressMap>(step: Step, arg?: TProgressMap[Step]): TaskObserverTask<TProgressMap[Step]>;
    task<Step extends keyof TProgressMap, TOut = any>(step: Step, fn: () => Promise<TOut> | TOut, arg?: TProgressMap[Step]): Promise<TOut>;
    onProgress(cb: TaskOnProgressFn<TProgressMap>): void;
}
export interface TaskStepIs<TProgressMap extends TaskProgressMap, OmitStep extends string | symbol | number> {
    is<TStep extends Exclude<keyof TProgressMap, OmitStep>>(step: TStep, cb: (arg: TProgressMap[TStep]) => void): TaskStepIs<Omit<TProgressMap, OmitStep>, TStep>;
}
export declare class TaskStep<TProgressMap extends TaskProgressMap, TStep extends keyof TProgressMap> extends Completable {
    readonly step: TStep;
    readonly arg: TProgressMap[TStep];
    constructor(step: TStep, arg: TProgressMap[TStep], events?: TaskStepEvents);
    is<TStep extends keyof TProgressMap>(step: TStep, cb: (arg: TProgressMap[TStep]) => void): TaskStepIs<TProgressMap, TStep>;
}
export {};
