<?php

namespace WriteTextAI\WriteTextAI\Plugin\Framework\Reflection;

use WriteTextAI\WriteTextAI\Api\Data\StoresLanguagesResultInterface;

class DataObjectProcessorPlugin
{
    /**
     * After plugin for buildOutputDataArray
     *
     * @param \Magento\Framework\Reflection\DataObjectProcessor $subject
     * @param array $result
     * @param mixed $dataObject
     * @param string $dataObjectType
     * @return array
     */
    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;
    }
}
