<?php

/**
 * Invalid Hub Accounts
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts;

use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstract_Shared_Base;
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Loggers\Log_Level_Enum;
use Throwable;
defined('ABSPATH') || exit;
/**
 * Class Invalid_Hub_Accounts
 */
class Invalid_Hub_Accounts
{
    public const OPTION_NAME = 'nmf_invalid_bo_accounts';
    /**
     * Get all accounts stored as invalid after saving settings.
     *
     * @return array
     */
    public static function get_invalid_accounts(): array
    {
        $invalid_accounts = \false;
        try {
            $invalid_accounts = get_option(self::OPTION_NAME, array());
        } catch (Throwable $e) {
            Abstract_Shared_Base::base_instance()->logger->log('hub-accounts', 'Failed to get invalid BO accounts', array('error' => $e->getMessage()), Log_Level_Enum::ERROR);
        }
        if (is_array($invalid_accounts)) {
            return $invalid_accounts;
        }
        Abstract_Shared_Base::base_instance()->logger->log('hub-accounts', 'Failed to get invalid BO accounts, not array. Resetting.', array('invalid_accounts' => $invalid_accounts), Log_Level_Enum::ERROR);
        self::clear_invalid_accounts();
        return array();
    }
    /**
     * Update invalid accounts.
     *
     * @param array $invalid_accounts Invalid accounts.
     *
     * @return void
     */
    public static function update_invalid_accounts(array $invalid_accounts): void
    {
        self::clear_invalid_accounts();
        update_option(self::OPTION_NAME, $invalid_accounts);
    }
    /**
     * Clear invalid accounts.
     */
    public static function clear_invalid_accounts(): void
    {
        delete_option(self::OPTION_NAME);
    }
}
