/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */
import { Snowflake } from 'nodejs-snowflake';
import { Transactionable } from 'sequelize';
import { Plugin } from '@nocobase/server';
import { Registry } from '@nocobase/utils';
import { Logger } from '@nocobase/logger';
import { EventOptions } from './Dispatcher';
import Processor, { ProcessorRerunOptions } from './Processor';
import ExecutionTimeoutManager from './ExecutionTimeoutManager';
import RunningExecutionRegistry from './RunningExecutionRegistry';
import { CustomFunction } from './functions';
import Trigger from './triggers';
import { InstructionInterface } from './instructions';
import type { ExecutionModel, JobModel, WorkflowModel } from './types';
import type { Transaction } from '@nocobase/database';
type ID = number | string;
type TaskStats = {
    pending: number;
    all: number;
};
export type TaskStatsRow = {
    userId: number;
    workflowKey: string;
    type: string;
    pending: number;
    all: number;
};
export type RepairTaskStatsOptions = {
    userIds?: number[];
    workflowKeys?: string[];
    types?: string[];
    transaction?: Transaction;
    silent?: boolean;
};
export type TaskStatsProvider = {
    collectTaskStats: (options: RepairTaskStatsOptions, context: {
        plugin: PluginWorkflowServer;
    }) => Promise<TaskStatsRow[]>;
};
export declare const WORKER_JOB_WORKFLOW_PROCESS = "workflow:process";
export default class PluginWorkflowServer extends Plugin {
    instructions: Registry<InstructionInterface>;
    triggers: Registry<Trigger>;
    functions: Registry<CustomFunction>;
    enabledCache: Map<number, WorkflowModel>;
    snowflake: Snowflake;
    private dispatcher;
    readonly timeoutManager: ExecutionTimeoutManager;
    readonly runningExecutionRegistry: RunningExecutionRegistry;
    get channelPendingExecution(): string;
    private loggerCache;
    private meter;
    private checker;
    private taskStatsProviders;
    private onBeforeSave;
    private onAfterCreate;
    private onAfterUpdate;
    private onAfterDestroy;
    private onAfterStart;
    private onBeforeStop;
    handleSyncMessage(message: any): Promise<void>;
    serving(): boolean;
    abortExecutionIfExpired(execution: ExecutionModel, options?: {
        transaction?: Transaction;
    }): Promise<boolean>;
    registerRunningExecution(executionId: number | string, abort: (reason?: string) => void): void;
    unregisterRunningExecution(executionId: number | string): void;
    abortRunningExecution(executionId: number | string, reason?: string): boolean;
    /**
     * @experimental
     */
    getLogger(workflowId?: ID): Logger;
    /**
     * @experimental
     * @param {WorkflowModel} workflow
     * @returns {boolean}
     */
    isWorkflowSync(workflow: WorkflowModel): boolean;
    registerTrigger<T extends Trigger>(type: string, trigger: T | {
        new (p: Plugin): T;
    }): void;
    registerInstruction(type: string, instruction: InstructionInterface | {
        new (p: Plugin): InstructionInterface;
    }): void;
    private initTriggers;
    private initInstructions;
    private registerErrorHandlers;
    beforeLoad(): Promise<void>;
    /**
     * @internal
     */
    load(): Promise<void>;
    private toggle;
    trigger(workflow: WorkflowModel, context: object, options?: EventOptions): void | Promise<Processor | null | void>;
    rerun(execution: ExecutionModel | ID, rerun?: ProcessorRerunOptions): Promise<void>;
    recover(): Promise<void>;
    /**
     * Persist the current job state and publish a poke that evaluates that state.
     *
     * PENDING resume calls are valid for instructions such as delay and sequential approval.
     * Callers that aggregate multiple user actions must only call this method when their
     * atomic update changes the job from PENDING to a terminal status.
     * Persistence and queue publication failures are logged and rethrown to awaiting callers.
     */
    resume(job: JobModel): Promise<void>;
    /**
     * Start a deferred execution
     * @experimental
     */
    start(execution: ExecutionModel | ID): Promise<void>;
    createProcessor(execution: ExecutionModel, options?: {}): Processor;
    private getModelId;
    execute(workflow: WorkflowModel, values: any, options?: EventOptions): Promise<void | Processor>;
    /**
     * @experimental
     * @param {string} dataSourceName
     * @param {Transaction} transaction
     * @param {boolean} create Create a new transaction when the input transaction does not belong to this data source.
     * @returns {Transaction}
     */
    useDataSourceTransaction(dataSourceName?: string, transaction?: Transaction | null, create?: false): Transaction | undefined;
    useDataSourceTransaction(dataSourceName: string | undefined, transaction: Transaction | null | undefined, create: true): Transaction | Promise<Transaction> | undefined;
    registerTaskStatsProvider(type: string, provider: TaskStatsProvider): void;
    getWorkflowIdsByKey(workflowKey: string, transaction?: Transaction): Promise<any>;
    private buildTaskStatsFilter;
    private normalizeTaskStats;
    private upsertTaskStatsRow;
    refreshUserWorkflowTaskTypeStats(userId: number, type: string, { transaction }?: Transactionable): Promise<any>;
    refreshUserWorkflowTaskWorkflowStats(userId: number, workflowKey: string, { transaction }?: Transactionable): Promise<any>;
    private sendTaskWorkflowStatsUpdated;
    updateTaskStatsByWorkflow(input: {
        userId: number;
        workflowKey: string;
        type: string;
        stats: TaskStats;
    }, { transaction }?: Transactionable): Promise<void>;
    repairTaskStats(options?: RepairTaskStatsOptions): Promise<void>;
    /**
     * @experimental
     * @deprecated Use updateTaskStatsByWorkflow() when workflowKey is available.
     */
    updateTasksStats(userId: number, type: string, stats: TaskStats, { transaction }: Transactionable): Promise<void>;
}
export {};
