<?php

/**
 * Vat account functions.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Sync\Northmill_Online_Shared\Modules\Sync\Bookkeeping\Vat_Account_Codes;

use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Hub_Account_Model;
use Northmill_Online_Shared_Sync\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Vat_Account_Code_Model;
defined('ABSPATH') || exit;
/**
 * Class Vat_Accounts
 */
class Vat_Accounts
{
    /**
     * Gets the vat account for the given rate
     *
     * @param Hub_Account_Model $backoffice_account Hub account to get code for.
     * @param int               $vat_rate 0, 6, 12, 25.
     *
     * @return int|null Account number, null if not set.
     */
    public static function get_vat_account(Hub_Account_Model $backoffice_account, int $vat_rate): ?int
    {
        $account_codes = $backoffice_account->get_vat_account_codes();
        if (empty($account_codes[$vat_rate]) || !is_a($account_codes[$vat_rate], Vat_Account_Code_Model::class)) {
            return null;
        }
        return $account_codes[$vat_rate]->get_account_code();
    }
    /**
     * Get account codes for default VAT rates.
     *
     * @param Hub_Account_Model $backoffice_account Hub account to get codes for.
     *
     * @return null[]|int[]
     */
    public static function get_default_account_codes(Hub_Account_Model $backoffice_account): array
    {
        return array(0 => self::get_vat_account($backoffice_account, 0), 6 => self::get_vat_account($backoffice_account, 6), 12 => self::get_vat_account($backoffice_account, 12), 25 => self::get_vat_account($backoffice_account, 25));
    }
}
