<?php

namespace Shm\ShmAdmin\AdminRPC;

use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAdmin\Utils\AdminLocalization;
use Shm\ShmRPC\ShmRPC;

class AdminRPCLocalizations
{
    public static function rpc()
    {

        return ShmRPC::lazy(function () {

            $adminLangItem = Shm::structure([
                'md5' => Shm::string(),
                'text' => Shm::string(),
            ])->staticBaseTypeName("LocalizationItem");

            return [
                'type' => Shm::arrayOf($adminLangItem),
                'args' => [
                    'texts' => Shm::arrayOf($adminLangItem),
                    'targetLang' => Shm::enum(AdminPanel::$allLangs),
                ],
                'resolve' => function ($root, $args) {
                    $targetLang = $args['targetLang'] ?? 'en';

                    return AdminLocalization::translateTexts($args['texts'], $targetLang);
                }

            ];
        });
    }
}
