<?php
/**
 * My Account page Elementor widgets — My Account tab toggles in plugin settings.
 *
 * Split layout: place widgets on the WooCommerce My Account page (or Theme Builder template).
 * Navigation is not included; use a separate menu or WC account navigation in the theme if needed.
 *
 * @package storzen
 */

namespace Elementor;

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

/**
 * Shared helpers for My Account widgets.
 */
trait Storzen_My_Account_Widget_Helpers {

	/**
	 * @return bool
	 */
	protected function szp_myaccount_wc_ready() {
		return function_exists( 'WC' );
	}

	/**
	 * @param string $label Placeholder label.
	 * @return bool True if placeholder was shown.
	 */
	protected function szp_myaccount_maybe_placeholder( $label ) {
		if ( $this->szp_myaccount_wc_ready() ) {
			return false;
		}
		$ed = \Elementor\Plugin::$instance->editor;
		if ( $ed && method_exists( $ed, 'is_edit_mode' ) && $ed->is_edit_mode() ) {
			printf(
				'<div class="szp-myaccount-widget-placeholder">%s</div>',
				esc_html( $label )
			);
			return true;
		}
		return false;
	}

	/**
	 * Open wrapper with skin class.
	 *
	 * @param string $extra Extra classes.
	 */
	protected function szp_myaccount_wrap_open( $extra = '' ) {
		$class = 'szp-myaccount-skin woocommerce ' . trim( $extra );
		echo '<div class="' . esc_attr( $class ) . '">';
	}

	/**
	 * Close wrapper.
	 */
	protected function szp_myaccount_wrap_close() {
		echo '</div>';
	}

	/**
	 * When the request is the My Account lost-password endpoint, render WC lost-password UI
	 * (form, confirmation, or reset form) — same as WC_Shortcode_My_Account::output.
	 *
	 * @return bool True if lost-password output was rendered (skip login form).
	 */
	protected function szp_myaccount_render_lost_password_if_endpoint() {
		global $wp;
		$is_lost = isset( $wp->query_vars['lost-password'] );
		if ( ! $is_lost && function_exists( 'WC' ) && WC()->query ) {
			$is_lost = ( 'lost-password' === WC()->query->get_current_endpoint() );
		}
		if ( ! $is_lost ) {
			return false;
		}
		if ( ! class_exists( '\WC_Shortcode_My_Account' ) ) {
			return false;
		}
		\WC_Shortcode_My_Account::lost_password();
		return true;
	}
}

/**
 * Account dashboard intro (WC myaccount/dashboard.php).
 */
class Storzen_My_Account_Dashboard_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-dashboard';
	}

	public function get_title() {
		return esc_html__( 'Account Dashboard', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-person';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}
		if ( ! is_user_logged_in() ) {
			if ( function_exists( 'wc_print_notice' ) ) {
				wc_print_notice( esc_html__( 'Please log in to view your dashboard.', 'storzen' ), 'notice' );
			}
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-dashboard' );
		wc_get_template(
			'myaccount/dashboard.php',
			array(
				'current_user' => get_user_by( 'id', get_current_user_id() ),
			)
		);
		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Orders list (WC orders template).
 */
class Storzen_My_Account_Orders_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-orders';
	}

	public function get_title() {
		return esc_html__( 'Account Orders', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-products';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}
		if ( ! is_user_logged_in() ) {
			if ( function_exists( 'wc_print_notice' ) ) {
				wc_print_notice( esc_html__( 'Please log in to view your orders.', 'storzen' ), 'notice' );
			}
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-orders' );
		$paged = absint( get_query_var( 'orders' ) );
		if ( $paged < 1 ) {
			$paged = 1;
		}
		if ( function_exists( 'woocommerce_account_orders' ) ) {
			woocommerce_account_orders( $paged );
		}
		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Billing / shipping addresses overview.
 */
class Storzen_My_Account_Addresses_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-addresses';
	}

	public function get_title() {
		return esc_html__( 'Account Addresses', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-map-pin';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}
		if ( ! is_user_logged_in() ) {
			if ( function_exists( 'wc_print_notice' ) ) {
				wc_print_notice( esc_html__( 'Please log in to manage addresses.', 'storzen' ), 'notice' );
			}
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-addresses' );
		wc_get_template( 'myaccount/my-address.php' );
		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Edit account details (name, email, password).
 */
class Storzen_My_Account_Details_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-details';
	}

	public function get_title() {
		return esc_html__( 'Account Details', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-lock-user';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}
		if ( ! is_user_logged_in() ) {
			if ( function_exists( 'wc_print_notice' ) ) {
				wc_print_notice( esc_html__( 'Please log in to edit account details.', 'storzen' ), 'notice' );
			}
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-details' );
		if ( function_exists( 'woocommerce_account_edit_account' ) ) {
			woocommerce_account_edit_account();
		}
		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Login / register form (for logged-out visitors).
 */
class Storzen_My_Account_Login_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-login';
	}

	public function get_title() {
		return esc_html__( 'Account Login', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-lock';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-login' );

		if ( is_user_logged_in() ) {
			echo '<div class="szp-myaccount-logged-in-msg">';
			echo esc_html__( 'You are logged in.', 'storzen' );
			echo ' <a href="' . esc_url( wc_get_page_permalink( 'myaccount' ) ) . '">' . esc_html__( 'Go to my account', 'storzen' ) . '</a>';
			echo '</div>';
			$this->szp_myaccount_wrap_close();
			return;
		}

		if ( $this->szp_myaccount_render_lost_password_if_endpoint() ) {
			$this->szp_myaccount_wrap_close();
			return;
		}

		if ( function_exists( 'woocommerce_login_form' ) ) {
			woocommerce_login_form(
				array(
					'redirect' => wc_get_page_permalink( 'myaccount' ),
				)
			);
		} else {
			wc_get_template( 'myaccount/form-login.php' );
		}

		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Wishlist (Webtend Wishlist Pro [wtwp_wishlist], YITH, or filter).
 */
class Storzen_My_Account_Wishlist_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-wishlist';
	}

	public function get_title() {
		return esc_html__( 'Account Wishlist', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-heart';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	protected function register_controls() {}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}

		$this->szp_myaccount_wrap_open( 'szp-myaccount-wishlist' );

		$html = '';
		if ( shortcode_exists( 'wtwp_wishlist' ) ) {
			$html = do_shortcode( apply_filters( 'storzen_myaccount_wishlist_shortcode', '[wtwp_wishlist]' ) );
		} elseif ( shortcode_exists( 'yith_wcwl_wishlist' ) ) {
			$html = do_shortcode( '[yith_wcwl_wishlist]' );
		}

		if ( $html ) {
			echo wp_kses_post( $html );
		} else {
			$ed = \Elementor\Plugin::$instance->editor;
			$is_edit = $ed && method_exists( $ed, 'is_edit_mode' ) && $ed->is_edit_mode();
			if ( $is_edit ) {
				echo '<div class="szp-myaccount-widget-placeholder szp-myaccount-widget-placeholder--warn">';
				echo esc_html__( 'Install Webtend Wishlist Pro (shortcode [wtwp_wishlist]) or YITH Wishlist to show the wishlist here.', 'storzen' );
				echo '</div>';
			} elseif ( is_user_logged_in() ) {
				if ( function_exists( 'wc_print_notice' ) ) {
					wc_print_notice( esc_html__( 'No wishlist plugin with a supported shortcode was found.', 'storzen' ), 'notice' );
				}
			} else {
				if ( function_exists( 'wc_print_notice' ) ) {
					wc_print_notice( esc_html__( 'Please log in to view your wishlist.', 'storzen' ), 'notice' );
				}
			}
		}

		$this->szp_myaccount_wrap_close();
	}
}

/**
 * Complete My Account widget with tabs navigation and all account sections.
 * This widget includes: Dashboard, Orders, Downloads, Addresses, Account Details, and Logout.
 */
class Storzen_My_Account_Full_Widget extends Widget_Base {
	use Storzen_My_Account_Widget_Helpers;

	public function get_name() {
		return 'storzen-my-account-full';
	}

	public function get_title() {
		return esc_html__( 'My Account (Full with Tabs)', 'storzen' );
	}

	public function get_icon() {
		return 'eicon-sitemap';
	}

	public function get_categories() {
		return array( 'storzen-my-account' );
	}

	public function get_style_depends() {
		return array( 'storzen-my-account-widgets' );
	}

	public function get_script_depends() {
		return array( 'storzen-my-account-tabs' );
	}

	protected function register_controls() {
		$this->start_controls_section(
			'layout_section',
			array(
				'label' => esc_html__( 'Layout', 'storzen' ),
			)
		);

		$this->add_control(
			'layout_style',
			array(
				'label'   => esc_html__( 'Layout Style', 'storzen' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'horizontal' => esc_html__( 'Horizontal (Tabs on Left)', 'storzen' ),
					'vertical'   => esc_html__( 'Vertical (Tabs on Top)', 'storzen' ),
				),
				'default' => 'horizontal',
			)
		);

		$this->add_responsive_control(
			'nav_width',
			array(
				'label'      => esc_html__( 'Navigation Width (%)', 'storzen' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 15,
						'max' => 40,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 25,
				),
				'selectors'  => array(
					'{{WRAPPER}} .szp-myaccount-full-nav' => 'width: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .szp-myaccount-full-content' => 'width: calc(100% - {{SIZE}}{{UNIT}});',
				),
				'condition'  => array(
					'layout_style' => 'horizontal',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'navigation_section',
			array(
				'label' => esc_html__( 'Navigation Tabs', 'storzen' ),
			)
		);

		$this->add_control(
			'show_dashboard',
			array(
				'label'        => esc_html__( 'Show Dashboard', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_orders',
			array(
				'label'        => esc_html__( 'Show Orders', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_downloads',
			array(
				'label'        => esc_html__( 'Show Downloads', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_addresses',
			array(
				'label'        => esc_html__( 'Show Addresses', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_details',
			array(
				'label'        => esc_html__( 'Show Account Details', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_payment_methods',
			array(
				'label'        => esc_html__( 'Show Payment Methods', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_wishlist',
			array(
				'label'        => esc_html__( 'Show Wishlist', 'storzen' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'storzen' ),
				'label_off'    => esc_html__( 'No', 'storzen' ),
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->end_controls_section();

		$this->add_style_controls();
	}

	protected function add_style_controls() {
		$this->start_controls_section(
			'navigation_style',
			array(
				'label' => esc_html__( 'Navigation Style', 'storzen' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'nav_typography',
				'selector' => '{{WRAPPER}} .szp-myaccount-full-nav a',
			)
		);

		$this->add_control(
			'nav_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .szp-myaccount-full-nav' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'nav_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .szp-myaccount-full-nav a' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'nav_active_color',
			array(
				'label'     => esc_html__( 'Active Tab Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .szp-myaccount-full-nav a.active, {{WRAPPER}} .szp-myaccount-full-nav a.is-active' => 'color: {{VALUE}}; border-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'nav_padding',
			array(
				'label'      => esc_html__( 'Padding', 'storzen' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .szp-myaccount-full-nav' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'content_style',
			array(
				'label' => esc_html__( 'Content Style', 'storzen' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'content_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'storzen' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .szp-myaccount-full-content' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'content_padding',
			array(
				'label'      => esc_html__( 'Padding', 'storzen' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .szp-myaccount-full-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function get_account_navigation_items( $settings ) {
		$items = array();

		if ( 'yes' === $settings['show_dashboard'] ) {
			$items['dashboard'] = esc_html__( 'Dashboard', 'storzen' );
		}

		if ( 'yes' === $settings['show_orders'] ) {
			$items['orders'] = esc_html__( 'Orders', 'storzen' );
		}

		if ( 'yes' === $settings['show_downloads'] && wc_get_page_id( 'shop' ) && get_option( 'woocommerce_enable_myaccount_downloads' ) !== 'no' ) {
			$items['downloads'] = esc_html__( 'Downloads', 'storzen' );
		}

		if ( 'yes' === $settings['show_addresses'] ) {
			$items['edit-address'] = esc_html__( 'Addresses', 'storzen' );
		}

		if ( 'yes' === $settings['show_payment_methods'] ) {
			$items['payment-methods'] = esc_html__( 'Payment methods', 'storzen' );
		}

		if ( 'yes' === $settings['show_details'] ) {
			$items['edit-account'] = esc_html__( 'Account details', 'storzen' );
		}

		if ( 'yes' === $settings['show_wishlist'] && ( shortcode_exists( 'wtwp_wishlist' ) || shortcode_exists( 'yith_wcwl_wishlist' ) ) ) {
			$items['wishlist'] = esc_html__( 'Wishlist', 'storzen' );
		}

		$items['customer-logout'] = esc_html__( 'Logout', 'storzen' );

		return apply_filters( 'storzen_myaccount_full_nav_items', $items, $settings );
	}

	protected function render() {
		if ( $this->szp_myaccount_maybe_placeholder( esc_html__( 'WooCommerce is required.', 'storzen' ) ) ) {
			return;
		}

		$settings     = $this->get_settings_for_display();
		$layout_style = $settings['layout_style'];
		$current_tab  = $this->get_current_endpoint();

		if ( ! is_user_logged_in() ) {
			$this->szp_myaccount_wrap_open( 'szp-myaccount-login' );
			if ( $this->szp_myaccount_render_lost_password_if_endpoint() ) {
				$this->szp_myaccount_wrap_close();
				return;
			}
			if ( function_exists( 'woocommerce_login_form' ) ) {
				woocommerce_login_form(
					array(
						'redirect' => wc_get_page_permalink( 'myaccount' ),
					)
				);
			} else {
				wc_get_template( 'myaccount/form-login.php' );
			}
			$this->szp_myaccount_wrap_close();
			return;
		}

		$nav_items = $this->get_account_navigation_items( $settings );

		echo '<div class="szp-myaccount-full woocommerce-account szp-myaccount-layout-' . esc_attr( $layout_style ) . ' woocommerce">';
		echo '<div class="szp-myaccount-full-wrapper">';

		echo '<nav class="szp-myaccount-full-nav woocommerce-MyAccount-navigation">';
		echo '<ul>';
		foreach ( $nav_items as $endpoint => $label ) {
			// Dashboard is not a real WC endpoint — use wc_get_account_endpoint_url() so the link
			// is the plain my-account permalink (see wc_get_account_endpoint_url in WC core).
			$url       = function_exists( 'wc_get_account_endpoint_url' )
				? wc_get_account_endpoint_url( $endpoint )
				: wc_get_endpoint_url( $endpoint, '', wc_get_page_permalink( 'myaccount' ) );
			$is_active = $current_tab === $endpoint;
			$class      = 'woocommerce-MyAccount-navigation-link woocommerce-MyAccount-navigation-link--' . esc_attr( $endpoint );
			$class     .= $is_active ? ' is-active' : '';

			printf(
				'<li class="%1$s"><a href="%2$s" class="%3$s" data-endpoint="%4$s">%5$s</a></li>',
				esc_attr( $class ),
				esc_url( $url ),
				$is_active ? 'active' : '',
				esc_attr( $endpoint ),
				esc_html( $label )
			);
		}
		echo '</ul>';
		echo '</nav>';

		echo '<div class="szp-myaccount-full-content woocommerce-MyAccount-content">';
		$this->render_account_content( $current_tab );
		echo '</div>';

		echo '</div>';
		echo '</div>';
	}

	protected function get_current_endpoint() {
		if ( ! function_exists( 'WC' ) || ! WC()->query ) {
			return 'dashboard';
		}

		$endpoint = WC()->query->get_current_endpoint();

		if ( '' === $endpoint ) {
			return 'dashboard';
		}

		// Map single-order view to the "Orders" tab in our custom nav.
		if ( 'view-order' === $endpoint ) {
			return 'orders';
		}

		return $endpoint;
	}

	protected function render_account_content( $current_tab ) {
		global $wp;

		switch ( $current_tab ) {
			case 'dashboard':
				wc_get_template(
					'myaccount/dashboard.php',
					array(
						'current_user' => get_user_by( 'id', get_current_user_id() ),
					)
				);
				break;

			case 'orders':
				$paged = absint( get_query_var( 'orders', 1 ) );
				if ( isset( $wp->query_vars['view-order'] ) ) {
					$order_id = absint( $wp->query_vars['view-order'] );
					wc_get_template( 'myaccount/view-order.php', array( 'order_id' => $order_id ) );
				} else {
					if ( function_exists( 'woocommerce_account_orders' ) ) {
						woocommerce_account_orders( $paged );
					}
				}
				break;

			case 'downloads':
				if ( function_exists( 'woocommerce_account_downloads' ) ) {
					woocommerce_account_downloads();
				}
				break;

			case 'edit-address':
				// Must use WooCommerce’s handler so `address` fields are built; the template
				// form-edit-address.php requires both `load_address` and `address` (see WC_Shortcode_My_Account::edit_address).
				$edit_address_var = isset( $wp->query_vars['edit-address'] ) ? $wp->query_vars['edit-address'] : '';
				$edit_address_var = is_scalar( $edit_address_var ) ? (string) $edit_address_var : '';
				if ( function_exists( 'woocommerce_account_edit_address' ) ) {
					woocommerce_account_edit_address( $edit_address_var );
				} elseif ( class_exists( '\WC_Shortcode_My_Account' ) ) {
					$type = function_exists( 'wc_edit_address_i18n' )
						? wc_edit_address_i18n( sanitize_title( $edit_address_var ), true )
						: sanitize_title( $edit_address_var );
					\WC_Shortcode_My_Account::edit_address( $type );
				}
				break;

			case 'payment-methods':
				if ( function_exists( 'woocommerce_account_payment_methods' ) ) {
					woocommerce_account_payment_methods();
				}
				break;

			case 'edit-account':
				if ( function_exists( 'woocommerce_account_edit_account' ) ) {
					woocommerce_account_edit_account();
				}
				break;

			case 'wishlist':
				if ( shortcode_exists( 'wtwp_wishlist' ) ) {
					echo do_shortcode( apply_filters( 'storzen_myaccount_wishlist_shortcode', '[wtwp_wishlist]' ) );
				} elseif ( shortcode_exists( 'yith_wcwl_wishlist' ) ) {
					echo do_shortcode( '[yith_wcwl_wishlist]' );
				}
				break;

			default:
				do_action( 'woocommerce_account_' . $current_tab . '_endpoint', $wp->query_vars[ $current_tab ] ?? '' );
				break;
		}
	}
}
