<?php
/**
 * Widget Name: Related Products
 * Description: Shows WooCommerce related products for the current or a chosen
 *              product as a customizable grid of product cards (image, title,
 *              rating, price, add to cart) with multiple attractive styles.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.3.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once __DIR__ . '/class-captwiki-woo-products-base.php';

/**
 * Class Captwiki_Woo_Related_Products
 *
 * @since 1.3.0
 */
class Captwiki_Woo_Related_Products extends Captwiki_Woo_Products_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-woo-related-products';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Related Products', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-products captwiki-el-logo';
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.3.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'woocommerce', 'woo', 'related', 'products', 'cross sell', 'recommended', 'captain-widgets-kit' );
	}

	/**
	 * Asset slug.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	protected function captwiki_css_slug(): string {
		return 'woo-related-products';
	}

	/**
	 * Default heading text.
	 *
	 * @since 1.3.0
	 * @return string
	 */
	protected function captwiki_default_heading(): string {
		return esc_html__( 'Related Products', 'captain-widgets-kit' );
	}

	/**
	 * Collect related product IDs.
	 *
	 * @since 1.3.0
	 * @param \WC_Product $product Source product.
	 * @param int         $limit   Maximum number of products.
	 * @return int[]
	 */
	protected function captwiki_get_product_ids( \WC_Product $product, int $limit ): array {

		if ( ! function_exists( 'wc_get_related_products' ) ) {
			return array();
		}

		return array_map( 'absint', wc_get_related_products( $product->get_id(), $limit ) );
	}
}
