<?php

declare(strict_types=1);

namespace Kylin\Pay\Shortcut\Paypal;

use Kylin\Pay\Artful\Contract\ShortcutInterface;
use Kylin\Pay\Artful\Exception\InvalidParamsException;
use Kylin\Pay\Artful\Plugin\AddPayloadBodyPlugin;
use Kylin\Pay\Artful\Plugin\ParserPlugin;
use Kylin\Pay\Artful\Plugin\StartPlugin;
use Kylin\Pay\Exception\Exception;
use Kylin\Pay\Plugin\Paypal\V2\AddRadarPlugin;
use Kylin\Pay\Plugin\Paypal\V2\ObtainAccessTokenPlugin;
use Kylin\Pay\Plugin\Paypal\V2\Pay\CapturePlugin;
use Kylin\Pay\Plugin\Paypal\V2\Pay\PayPlugin;
use Kylin\Pay\Plugin\Paypal\V2\ResponsePlugin;

class WebShortcut implements ShortcutInterface
{
    /**
     * @throws InvalidParamsException
     */
    public function getPlugins(array $params): array
    {
        $action = $params['_action'] ?? 'default';

        return match ($action) {
            'default', 'pay' => $this->payPlugins(),
            'capture' => $this->capturePlugins(),
            default => throw new InvalidParamsException(Exception::PARAMS_SHORTCUT_ACTION_INVALID, "不支持的 PayPal Web 操作: [{$action}]"),
        };
    }

    protected function payPlugins(): array
    {
        return [
            StartPlugin::class,
            ObtainAccessTokenPlugin::class,
            PayPlugin::class,
            AddPayloadBodyPlugin::class,
            AddRadarPlugin::class,
            ResponsePlugin::class,
            ParserPlugin::class,
        ];
    }

    protected function capturePlugins(): array
    {
        return [
            StartPlugin::class,
            ObtainAccessTokenPlugin::class,
            CapturePlugin::class,
            AddPayloadBodyPlugin::class,
            AddRadarPlugin::class,
            ResponsePlugin::class,
            ParserPlugin::class,
        ];
    }
}
