Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

1022 B
Raw Blame
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.

import type { ConditionPath } from "./schema";

export type PathPosition =
  | { kind: "else" }
  | { kind: "indexed"; index: number };

/**
 * Positional identity for a condition path. Returns `{ kind: "else" }` for the
 * else path and `{ kind: "indexed", index }` for if/elseIf paths (in order),
 * so two elseIf paths never share a label. An unknown pathId returns index: -1.
 */
export function pathLabel(
  paths: ConditionPath[],
  pathId: string
): PathPosition {
  const path = paths.find((p) => p.id === pathId);
  if (!path) return { kind: "indexed", index: -1 };
  if (path.kind === "else") return { kind: "else" };
  const index = paths
    .filter((p) => p.kind !== "else")
    .findIndex((p) => p.id === pathId);
  return { kind: "indexed", index };
}