<?php

declare (strict_types=1);
namespace PoPCMSSchema\CustomPostMutations\MutationResolvers;

use PoPCMSSchema\CustomPostMutations\Constants\CustomPostCRUDHookNames;
use PoPCMSSchema\CustomPostMutations\FeedbackItemProviders\MutationErrorFeedbackItemProvider;
use PoPCMSSchema\CustomPostMutations\ObjectModels\CustomPostAncestorRecursionErrorPayload;
use PoPCMSSchema\CustomPostMutations\ObjectModels\CustomPostDoesNotExistErrorPayload;
use PoPCMSSchema\CustomPostMutations\ObjectModels\CustomPostDoesNotHaveExpectedTypeErrorPayload;
use PoPCMSSchema\CustomPostMutations\ObjectModels\LoggedInUserHasNoEditingCustomPostCapabilityErrorPayload;
use PoPCMSSchema\CustomPostMutations\ObjectModels\LoggedInUserHasNoPermissionToEditCustomPostErrorPayload;
use PoPCMSSchema\CustomPostMutations\ObjectModels\LoggedInUserHasNoPublishingCustomPostCapabilityErrorPayload;
use PoPCMSSchema\UserStateMutations\ObjectModels\UserIsNotLoggedInErrorPayload;
use PoPSchema\SchemaCommons\ObjectModels\ErrorPayloadInterface;
use PoPSchema\SchemaCommons\ObjectModels\GenericErrorPayload;
use PoP\ComponentModel\App;
use PoP\ComponentModel\Feedback\ObjectTypeFieldResolutionFeedbackInterface;
/** @internal */
trait PayloadableCustomPostMutationResolverTrait
{
    protected function createErrorPayloadFromObjectTypeFieldResolutionFeedback(ObjectTypeFieldResolutionFeedbackInterface $objectTypeFieldResolutionFeedback) : ErrorPayloadInterface
    {
        $feedbackItemResolution = $objectTypeFieldResolutionFeedback->getFeedbackItemResolution();
        return match ([$feedbackItemResolution->getFeedbackProviderServiceClass(), $feedbackItemResolution->getCode()]) {
            [$this->getUserNotLoggedInErrorFeedbackItemProviderClass(), $this->getUserNotLoggedInErrorFeedbackItemProviderCode()] => new UserIsNotLoggedInErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E2] => new LoggedInUserHasNoEditingCustomPostCapabilityErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E3] => new LoggedInUserHasNoPublishingCustomPostCapabilityErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E8] => new LoggedInUserHasNoPermissionToEditCustomPostErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E9] => new LoggedInUserHasNoPermissionToEditCustomPostErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E7] => new CustomPostDoesNotExistErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E10] => new CustomPostDoesNotExistErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E11] => new CustomPostAncestorRecursionErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E12] => new CustomPostAncestorRecursionErrorPayload($feedbackItemResolution->getMessage()),
            [MutationErrorFeedbackItemProvider::class, MutationErrorFeedbackItemProvider::E5] => new CustomPostDoesNotHaveExpectedTypeErrorPayload($feedbackItemResolution->getMessage()),
            // Allow components (eg: CustomPostCategoryMutations) to inject their own validations
            default => App::applyFilters(CustomPostCRUDHookNames::ERROR_PAYLOAD, new GenericErrorPayload($feedbackItemResolution->getMessage(), $feedbackItemResolution->getNamespacedCode()), $objectTypeFieldResolutionFeedback),
        };
    }
    protected function getUserNotLoggedInErrorFeedbackItemProviderClass() : string
    {
        return MutationErrorFeedbackItemProvider::class;
    }
    protected function getUserNotLoggedInErrorFeedbackItemProviderCode() : string
    {
        return MutationErrorFeedbackItemProvider::E1;
    }
}
