<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpPositions\Pages;

use App\Filament\Resources\DberpPositions\DberpPositionResource;
use App\Filament\Resources\Pages\EditRecord;

class EditDberpPosition extends EditRecord
{
    protected static string $resource = DberpPositionResource::class;

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

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

        return $data;
    }
}
