<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpDepartments\Pages;

use App\Filament\Resources\DberpDepartments\DberpDepartmentResource;
use App\Filament\Resources\Pages\CreateRecord;

class CreateDberpDepartment extends CreateRecord
{
    protected static string $resource = DberpDepartmentResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        if (array_key_exists('code', $data) && is_string($data['code']) && trim($data['code']) === '') {
            $data['code'] = null;
        }

        if (array_key_exists('parent_id', $data) && ($data['parent_id'] === '' || $data['parent_id'] === null)) {
            $data['parent_id'] = null;
        }

        return $data;
    }
}
