<?php

namespace GriffinForms\Includes\Packages;

use GriffinForms\Traits\Singleton;

class PackageApproval extends \GriffinForms\Format
{
    use Singleton;

    protected $registry;

    protected function setRegistry()
    {
        $this->registry = PackageRegistry::getInstance();
    }

    public function getApprovalStatus(array $package): string
    {
        $package_id = sanitize_key((string) ($package['package_id'] ?? ''));
        $package_source = sanitize_key((string) ($package['package_source'] ?? ''));
        $bootstrap_file = (string) ($package['package_bootstrap_file'] ?? '');

        if ($package_id === 'core') {
            return 'approved';
        }

        if ($package_id === 'pro'
            && $package_source === 'griffinforms_internal'
            && $bootstrap_file === 'griffinforms-form-builder-pro/griffinforms-form-builder-pro.php'
        ) {
            return 'approved';
        }

        if ($package_source === 'griffinforms_first_party') {
            return 'approved';
        }

        if ($package_source === 'griffinforms_internal') {
            return 'internal_only';
        }

        return 'unapproved';
    }

    public function isApproved(array $package): bool
    {
        return $this->getApprovalStatus($package) === 'approved';
    }
}
