package pipeline

import (
	"fmt"

	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
)

func validateAndMaterializeChunkPlan(doc *source.SourceDocument, plan source.ChunkPlan) (source.ChunkPlan, []source.Chunk, error) {
	canonical, err := source.CanonicalizeChunkPlan(plan)
	if err != nil {
		return source.ChunkPlan{}, nil, fmt.Errorf("canonicalize chunk plan: %w", err)
	}
	if err := source.ValidateChunkPlan(doc, canonical); err != nil {
		return source.ChunkPlan{}, nil, err
	}
	chunks, err := source.MaterializeChunkPlan(doc, canonical)
	if err != nil {
		return source.ChunkPlan{}, nil, err
	}
	return canonical, chunks, nil
}

func cloneSourceUnit(unit source.SourceUnit) (source.SourceUnit, error) {
	metadata, err := cloneMetadata(unit.Metadata)
	if err != nil {
		return source.SourceUnit{}, fmt.Errorf("clone source unit %d metadata: %w", unit.ID, err)
	}
	return source.SourceUnit{
		ID:       unit.ID,
		Kind:     unit.Kind,
		Text:     unit.Text,
		Ref:      unit.Ref,
		Metadata: metadata,
	}, nil
}
