<?php

namespace App\Modules\Exports\Services;

use App\Modules\Exports\ExportHandlers\Contracts\ExportHandlerInterface;
use InvalidArgumentException;

class ExportHandlerRegistry
{
    /** @var array<string, class-string<ExportHandlerInterface>> */
    private array $map;

    public function __construct()
    {
        $this->map = (array) config('exports.handlers', []);
    }

    public function handler(string $type): ExportHandlerInterface
    {
        $class = $this->map[$type] ?? null;
        if ($class === null || ! class_exists($class)) {
            throw new InvalidArgumentException('Unknown export type: '.$type);
        }

        return app($class);
    }
}
