<?php

namespace App\Modules\Settings\Localization\DTOs;

final readonly class LanguageData
{
    public function __construct(
        public string $code,
        public string $name,
        public ?string $nativeName,
        public bool $isRtl,
        public bool $isActive,
    ) {}

    /**
     * @param  array{code: string, name: string, native_name?: ?string, is_rtl?: bool, is_active?: bool}  $data
     */
    public static function fromValidated(array $data): self
    {
        return new self(
            code: strtolower(trim($data['code'])),
            name: trim($data['name']),
            nativeName: isset($data['native_name']) ? trim($data['native_name']) : null,
            isRtl: (bool) ($data['is_rtl'] ?? false),
            isActive: (bool) ($data['is_active'] ?? true),
        );
    }

    /**
     * @return array{code: string, name: string, native_name: ?string, is_rtl: bool, is_active: bool}
     */
    public function toArray(): array
    {
        return [
            'code' => $this->code,
            'name' => $this->name,
            'native_name' => $this->nativeName,
            'is_rtl' => $this->isRtl,
            'is_active' => $this->isActive,
        ];
    }
}
