// Copyright 2020-2026 Project Capsule Authors
// SPDX-License-Identifier: Apache-2.0

package processor

import (
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

	"github.com/projectcapsule/capsule/pkg/runtime/gvk"
)

// Keeps track of generated items.
type Accumulator = map[string]*AccumulatorItem

// Keeps track of generated items.
type AccumulatorItem struct {
	Resource gvk.ResourceID
	Objects  *[]AccumulatorObject
}

// Keeps track of generated items.
type AccumulatorObject struct {
	Origin gvk.TenantResourceIDWithOrigin
	Object *unstructured.Unstructured
}

func AccumulatorAdd(
	acc Accumulator,
	resource gvk.ResourceID,
	obj AccumulatorObject,
) {
	if acc == nil {
		return
	}

	key := resource.GetKey("")

	if entry, ok := acc[key]; ok && entry != nil {
		if entry.Objects == nil {
			list := make([]AccumulatorObject, 0, 1)
			entry.Objects = &list
		}

		*entry.Objects = append(*entry.Objects, obj)

		return
	}

	list := []AccumulatorObject{obj}

	acc[key] = &AccumulatorItem{
		Resource: resource,
		Objects:  &list,
	}
}
