<?php
/**
 * Silent installer skin for REST package uploads (no admin HTML output).
 *
 * @package FleekDash\Controllers\Plugins
 */

namespace FleekDash\Controllers\Plugins;

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';

/**
 * Captures upgrader feedback without rendering wp-admin markup.
 */
class PackageInstallerSkin extends \Automatic_Upgrader_Skin
{
    /** @var string Plugin/theme overwrite mode from WP core upload flow. */
    public $overwrite = '';

    /** @var string Install source type expected by Plugin_Upgrader::install_strings(). */
    public $type = 'upload';

    /**
     * @param array<string, mixed> $args
     */
    public function __construct(array $args = [])
    {
        $this->overwrite = isset($args['overwrite']) ? (string) $args['overwrite'] : '';
        $this->type = isset($args['type']) ? (string) $args['type'] : 'upload';
        parent::__construct();
    }

    /**
     * @return \WP_Error|null
     */
    public function getResultError(): ?\WP_Error
    {
        if (!isset($this->result) || !\is_wp_error($this->result)) {
            return null;
        }

        return $this->result;
    }
}
