<?php

namespace WriteTextAI\WriteTextAI\Plugin\Framework\Reflection;

use WriteTextAI\WriteTextAI\Api\Data\StoresLanguagesResultInterface;

class DataObjectProcessorPlugin
{
    public function afterBuildOutputDataArray(
        \Magento\Framework\Reflection\DataObjectProcessor $subject,
        array $result,
        $dataObject,
        $dataObjectType
    ): array {
        $shouldProcess = false;
        if ($dataObjectType === StoresLanguagesResultInterface::class) {
            $shouldProcess = true;
        } elseif (is_string($dataObjectType) && is_a($dataObjectType, StoresLanguagesResultInterface::class, true)) {
            $shouldProcess = true;
        } elseif (is_object($dataObject) && $dataObject instanceof StoresLanguagesResultInterface) {
            $shouldProcess = true;
        }

        if (!$shouldProcess) {
            return $result;
        }

        if (isset($result['store_ids']) && !isset($result['storeIds'])) {
            $result['storeIds'] = $result['store_ids'];
            unset($result['store_ids']);
        }

        return $result;
    }
}
