<?php

// If this file is called directly, abort.
if (!defined('WPINC')) {
	die;
}

if (!class_exists('ProductFormatter')) {
	require_once ASWP_ATHENA_CORE_INC . 'Controller/ProductFormatter.php';
}

class ProductsController {

	public function getProductsCount(): int {
		if (!function_exists('wc_get_products'))
			return 0;
		$ids = wc_get_products([
			'limit' => -1,
			'return' => 'ids'
		]);
		return is_array($ids) ? count($ids) : 0;
	}

	// Get product ids for batch (limit, offset)
	public function getProductIds(int $limit, int $offset = 0): array {
		if (!function_exists('wc_get_products'))
			return [];
		$ids = wc_get_products([
			'limit' => $limit,
			'offset' => $offset,
			'return' => 'ids'
		]);
		return is_array($ids) ? $ids : [];
	}

	// Get formatted products by ids
	public function getProductsByIds(array $ids): array
	{
		if (empty($ids) || !function_exists('wc_get_products'))
			return [];
		$wc = wc_get_products(['include' => $ids, 'limit' => -1]);
		$products = [];
		foreach ($wc as $p) {
			$products[] = ProductFormatter::format($p);
		}
		return $products;
	}
}