import * as T from '../types'
import * as IST from '../actions/input/inputSpecTypes'
import { Action, ActionInfo } from './setupActions'
import { ExtractInputSpecType } from './input/builder/inputSpec'
export type RunActionInput =
| Input
| ((prev?: { spec: IST.InputSpec; value: Input | null }) => Input)
export const runAction = async <
Input extends Record,
>(options: {
effects: T.Effects
// packageId?: T.PackageId
actionId: T.ActionId
input?: RunActionInput
}) => {
if (options.input) {
if (options.input instanceof Function) {
const prev = await options.effects.action.getInput({
// packageId: options.packageId,
actionId: options.actionId,
})
const input = options.input(
prev
? { spec: prev.spec as IST.InputSpec, value: prev.value as Input }
: undefined,
)
return options.effects.action.run({
// packageId: options.packageId,
actionId: options.actionId,
input,
})
} else {
return options.effects.action.run({
// packageId: options.packageId,
actionId: options.actionId,
input: options.input,
})
}
} else {
return options.effects.action.run({
// packageId: options.packageId,
actionId: options.actionId,
})
}
}
type GetActionInputType> =
A extends Action ? I : never
type TaskBase = {
reason?: string
replayId?: string
}
type TaskInput> = {
kind: 'partial'
value: T.DeepPartial>
}
export type TaskOptions> = TaskBase &
(
| {
when?: Exclude
input?: TaskInput
}
| {
when: T.TaskTrigger & { condition: 'input-not-matches' }
input: TaskInput
}
)
const _validate: T.Task = {} as TaskOptions & {
actionId: string
packageId: string
severity: T.TaskSeverity
}
export const createTask = >(options: {
effects: T.Effects
packageId: T.PackageId
action: T
severity: T.TaskSeverity
options?: TaskOptions
}) => {
const request = options.options || {}
const actionId = options.action.id
const req = {
...request,
actionId,
packageId: options.packageId,
action: undefined,
severity: options.severity,
replayId: request.replayId || `${options.packageId}:${actionId}`,
}
delete req.action
return options.effects.action.createTask(req)
}