<?php
/**
 * Elementor Show Catalog widget class.
 *
 * @package ecommerce-product-catalog
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Elementor widget that inserts catalog content into the page.
 */
class Elementor_IC_Show_Catalog_Widget extends \Elementor\Widget_Base {

	/**
	 * Retrieve the widget slug.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'ic_show_catalog';
	}

	/**
	 * Retrieve the widget title.
	 *
	 * @return string
	 */
	public function get_title() {
		return __( 'Show Catalog', 'ecommerce-product-catalog' );
	}

	/**
	 * Retrieve the widget icon.
	 *
	 * @return string
	 */
	public function get_icon() {
		return 'dashicons dashicons-store';
	}

	/**
	 * Retrieve the widget categories.
	 *
	 * @return array
	 */
	public function get_categories() {
		return array( 'implecode' );
	}

	/**
	 * Registers widget controls.
	 *
	 * @return void
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'content_section',
			array(
				'label' => __( 'Info', 'plugin-name' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'url',
			array(
				'label' => __( 'Catalog Markup Element', 'ecommerce-product-catalog' ),
				'type'  => \Elementor\Controls_Manager::RAW_HTML,
				/* translators: 1: opening catalog settings link, 2: closing catalog settings link, 3: opening archive names link, 4: closing archive names link. */
				'raw'   => '<br>' . __( 'Use this to position your catalog container. The catalog container will show different content for product listing, category page, single product page and search results.', 'plugin-name' ) . '<br><br>' . __( 'By default it shows the main catalog page content.', 'ecommerce-product-catalog' ) . '<br><br>' . sprintf( __( 'Go to %1$scatalog settings%2$s to adjust what is visible here.', 'ecommerce-product-catalog' ), '<a target="_blank" href="' . esc_url( admin_url( 'edit.php?post_type=al_product&page=product-settings.php' ) ) . '">', '</a>' ) . '<br><br>' . sprintf( __( '%1$sHere%2$s you can edit all text that is displayed inside the catalog container.', 'ecommerce-product-catalog' ), '<a target="_blank" href="' . esc_url( admin_url( 'edit.php?post_type=al_product&page=product-settings.php&tab=names-settings&submenu=archive-names' ) ) . '">', '</a>' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Renders the widget output on the frontend.
	 *
	 * @return void
	 */
	protected function render() {
		global $ic_shortcode_catalog;
		if ( ! empty( $ic_shortcode_catalog ) ) {
			if ( $ic_shortcode_catalog->is_page_builder_edit() ) {
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML is generated by the catalog shortcode renderer.
				echo $ic_shortcode_catalog->catalog_shortcode();
			} else {
				echo '[show_product_catalog]';
			}
		}
	}

	/**
	 * Declares that the widget uses dynamic content.
	 *
	 * @return bool
	 */
	protected function is_dynamic_content(): bool {
		return true;
	}
}
