<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpEmployees\Pages;

use App\Filament\Resources\DberpEmployees\DberpEmployeeResource;
use App\Filament\Resources\Pages\CreateRecord;
use App\Filament\Support\DberpFormSaveSubheading;
use Illuminate\Contracts\Support\Htmlable;

class CreateDberpEmployee extends CreateRecord
{
    protected static string $resource = DberpEmployeeResource::class;

    public function getSubheading(): string|Htmlable|null
    {
        return DberpFormSaveSubheading::append((string) __('dberp.ui.organization_list_employee_intro'));
    }

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

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

        return $data;
    }
}
