<?php

namespace Barn2\Plugin\WC_Product_Table;

use Barn2\Plugin\WC_Product_Table\Util\Util;
use Barn2\Plugin\WC_Product_Table\Dependencies\Barn2\Table_Generator\Shortcode;

/**
 * A Product_Table factory.
 *
 * @package   Barn2\woocommerce-product-table
 * @author    Barn2 Plugins <support@barn2.com>
 * @license   GPL-3.0
 * @copyright Barn2 Media Ltd
 */
class Table_Factory {


	private static $tables     = [];
	private static $current_id = 1;

	/**
	 * Create a new table based on the supplied args.
	 *
	 * @param  array $args The args to use for the table.
	 * @return Product_Table The product table object.
	 */
	public static function create( array $args = [] ) {

		// Get table builder arguments if table id is present.
		$table_id = apply_filters( 'wc_product_table_args_id', $args['id'] ?? null );
		if ( $table_id && $table = Util::get_content_table( $table_id ) ) {
			$args = wp_parse_args( Shortcode::get_parameters( $table, $args, Table_Args::class ), $args );
		}

		// Merge in the default args, so our table ID reflects the full list of args including plugin settings.
		$args = apply_filters( 'wc_product_table_args', wp_parse_args( $args, Table_Args::get_defaults() ) );
		$id   = self::generate_id( $args );

		$table               = new Product_Table( $id, $args );
		self::$tables[ $id ] = $table;

		return $table;
	}

	/**
	 * Fetch an existing table by ID.
	 *
	 * @param  string $id The product table ID.
	 * @return Product_Table|false The product table object.
	 */
	public static function fetch( $id ) {
		if ( empty( $id ) ) {
			return false;
		}

		$table = false;

		if ( isset( self::$tables[ $id ] ) ) {
			$table = self::$tables[ $id ];
		} elseif ( $table = Table_Cache::get_table( $id ) ) {
			self::$tables[ $id ] = $table;
		}

		return $table;
	}

	private static function generate_id( array $args ) {
		$id = 'wcpt_' . substr( md5( serialize( $args ) ), 0, 16 ) . '_' . self::$current_id;
		++self::$current_id;

		return $id;
	}
}
