<?php

declare(strict_types=1);

namespace App\Filament\Resources\Articles\Pages;

use App\Domain\Plugin\Hook;
use App\Filament\Resources\Articles\ArticleResource;
use Filament\Resources\Pages\CreateRecord;

class CreateArticle extends CreateRecord
{
    protected static string $resource = ArticleResource::class;

    /**
     * @param  array<string, mixed>  $data
     * @return array<string, mixed>
     */
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $filtered = Hook::filter('filament.article.mutate_before_save', $data, null);
        $data = is_array($filtered) ? $filtered : $data;

        $validated = Hook::filter('filament.article.validate_access_restrictions', $data, null);
        $data = is_array($validated) ? $validated : $data;

        unset($data['paid_content'], $data['membership']);

        return $data;
    }

    protected function afterCreate(): void
    {
        Hook::dispatch('filament.article.after_save', $this->record, $this->form->getState());
    }
}
