30 lines
1.4 KiB
TypeScript
30 lines
1.4 KiB
TypeScript
export const AUTOMATION_CONDITION_BRANCHES_CONFIG_KEY = "__automationConditionBranches" as const;
|
|
|
|
export type AutomationConditionBranch = "true" | "false";
|
|
|
|
export function automationConditionBranches(config: Record<string, unknown>): Readonly<Record<string, AutomationConditionBranch>> {
|
|
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<string, unknown>).filter((entry): entry is [string, AutomationConditionBranch] => entry[1] === "true" || entry[1] === "false")
|
|
);
|
|
}
|
|
|
|
export function automationConditionBranchForParent(config: Record<string, unknown>, parentStepKey: string): AutomationConditionBranch {
|
|
return automationConditionBranches(config)[parentStepKey] ?? "true";
|
|
}
|
|
|
|
export function withAutomationConditionBranch(
|
|
config: Record<string, unknown>,
|
|
parentStepKey: string,
|
|
branch: AutomationConditionBranch | null
|
|
): Record<string, unknown> {
|
|
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;
|
|
}
|