<?php

/**
 * Factory for article account codes.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Sync\Bookkeeping\Article_Account_Codes;

use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Abstracts\Hub_API\Hub_Accounts\Models\Article_Account_Code_Model;
defined('ABSPATH') || exit;
/**
 * Class Factory
 */
class Factory
{
    /**
     * Create article account code from API data.
     *
     * @param mixed $api_data Data from API.
     *
     * @return Article_Account_Code_Model[]
     */
    public static function create_array_from_api(mixed $api_data): array
    {
        if (empty($api_data) || !is_array($api_data)) {
            return array();
        }
        $nmf_tax_classes = array();
        foreach ($api_data as $tax_class) {
            $name = Article_Account_Codes::WC_TAX_CLASS_NAME_PREFIX . $tax_class->AccountCode->Name;
            $nmf_tax_classes[$tax_class->AccountCode->Id] = new Article_Account_Code_Model(array('id' => (string) $tax_class->AccountCode->Id, 'name' => $name, 'slug' => sanitize_title($name), 'description' => (string) $tax_class->AccountCode->Description, 'vat_rate' => floatval($tax_class->AccountCode->VatRate), 'account_code' => intval($tax_class->AccountCode->Account), 'cost_center' => (string) $tax_class->AccountCode->CostCenter, 'project' => (string) $tax_class->AccountCode->Project));
        }
        return $nmf_tax_classes;
    }
    /**
     * Create article account code from API data.
     *
     * @param string $tax_classes_option_json JSON string of tax classes.
     *
     * @return Article_Account_Code_Model[]
     */
    public static function create_array_from_options(string $tax_classes_option_json): array
    {
        if (empty($tax_classes_option_json)) {
            return array();
        }
        $tax_classes_option = json_decode($tax_classes_option_json, \true);
        if (!is_array($tax_classes_option)) {
            return array();
        }
        $class_array = array();
        foreach ($tax_classes_option as $tax_class) {
            $tax_class_id = $tax_class['id'];
            $class_array[$tax_class_id] = new Article_Account_Code_Model($tax_class);
        }
        return $class_array;
    }
}
