export const AUTOMATION_CONDITION_BRANCHES_CONFIG_KEY = "__automationConditionBranches" as const; export type AutomationConditionBranch = "true" | "false"; export function automationConditionBranches(config: Record): Readonly> { const value = config[AUTOMATION_CONDITION_BRANCHES_CONFIG_KEY]; if (!value || typeof value !== "object" || Array.isArray(value)) return {}; return Object.fromEntries( Object.entries(value as Record).filter((entry): entry is [string, AutomationConditionBranch] => entry[1] === "true" || entry[1] === "false") ); } export function automationConditionBranchForParent(config: Record, parentStepKey: string): AutomationConditionBranch { return automationConditionBranches(config)[parentStepKey] ?? "true"; } export function withAutomationConditionBranch( config: Record, parentStepKey: string, branch: AutomationConditionBranch | null ): Record { const branches = { ...automationConditionBranches(config) }; if (branch === null) delete branches[parentStepKey]; else branches[parentStepKey] = branch; const next = { ...config }; if (Object.keys(branches).length === 0) delete next[AUTOMATION_CONDITION_BRANCHES_CONFIG_KEY]; else next[AUTOMATION_CONDITION_BRANCHES_CONFIG_KEY] = branches; return next; }