import { handleApiError } from "./api-error.js";
export function isWorkflow(binding) {
    return typeof binding === "object" && binding.type === "workflow";
}
/**
 * Creates a workflow binding for orchestrating and automating tasks.
 *
 * @example
 * ```ts
 * // Create a basic workflow
 * const workflow = Workflow("my-workflow", {
 *   workflowName: "my-workflow",
 *   className: "MyWorkflow"
 * });
 * ```
 */
export function Workflow(id, props = {}) {
    const workflowName = props.workflowName ?? props.className ?? id;
    const className = props.className ?? workflowName;
    return {
        type: "workflow",
        _PARAMS: undefined,
        id,
        workflowName,
        className,
        scriptName: props.scriptName,
        limits: props.limits,
    };
}
export async function upsertWorkflow(api, props) {
    const response = await api.put(`/accounts/${api.accountId}/workflows/${props.workflowName}`, {
        class_name: props.className,
        script_name: props.scriptName,
    });
    if (!response.ok) {
        await handleApiError(response, "create", "workflow", props.workflowName);
    }
    const body = (await response.json());
    return body.result;
}
export async function deleteWorkflow(api, name) {
    const response = await api.delete(`/accounts/${api.accountId}/workflows/${name}`);
    if (!response.ok && response.status !== 404) {
        await handleApiError(response, "delete", "workflow", name);
    }
}
//# sourceMappingURL=workflow.js.map