<?php

declare (strict_types=1);
namespace PoP\ComponentModel\DirectiveResolvers;

use PoP\ComponentModel\Engine\EngineIterationFieldSet;
/** @internal */
trait RemoveIDFieldSetFieldDirectiveResolverTrait
{
    /**
     * @param array<array<string|int,EngineIterationFieldSet>> $succeedingPipelineIDFieldSet
     * @param array<string|int,EngineIterationFieldSet> $idFieldSetToRemove
     */
    protected function removeIDFieldSet(array &$succeedingPipelineIDFieldSet, array $idFieldSetToRemove) : void
    {
        // For each combination of ID and field, remove them from the upcoming pipeline stages
        foreach ($idFieldSetToRemove as $id => $fieldSet) {
            foreach ($succeedingPipelineIDFieldSet as &$pipelineStageIDFieldSet) {
                // The next stage may or may not deal with this ID
                if (!\array_key_exists($id, $pipelineStageIDFieldSet)) {
                    continue;
                }
                // @phpstan-ignore-next-line
                $pipelineStageIDFieldSet[$id]->fields = \array_values(\array_diff($pipelineStageIDFieldSet[$id]->fields, $fieldSet->fields));
                foreach ($fieldSet->fields as $removeField) {
                    $pipelineStageIDFieldSet[$id]->conditionalFields->offsetUnset($removeField);
                }
                if ($pipelineStageIDFieldSet[$id]->fields === []) {
                    unset($pipelineStageIDFieldSet[$id]);
                }
            }
        }
    }
}
