// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

import type { ProvisionResult } from '../../provision/types.ts'
import type { Session } from '../session/session.ts'
import { rollupList, rollupPipe } from '../../provision/rollup.ts'

export type ProvisionNodeFn = (node: unknown, session: Session) => Promise<ProvisionResult>

export async function handlePipeProvision(
  provisionNode: ProvisionNodeFn,
  commands: readonly unknown[],
  session: Session,
): Promise<ProvisionResult> {
  const children: ProvisionResult[] = []
  for (const cmd of commands) children.push(await provisionNode(cmd, session))
  return rollupPipe(children)
}

export async function handleConnectionProvision(
  provisionNode: ProvisionNodeFn,
  left: unknown,
  op: string,
  right: unknown,
  session: Session,
): Promise<ProvisionResult> {
  const children: ProvisionResult[] = []
  children.push(await provisionNode(left, session))
  children.push(await provisionNode(right, session))
  return rollupList(op, children)
}
