<?php
/**
 * Off-canvas sidebar menu.
 *
 * @package storecraft
 * @since 1.0
 */

$storecraft_sidebar_menu_location = '';
if ( has_nav_menu( 'primary' ) ) {
	$storecraft_sidebar_menu_location = 'primary';
} elseif ( has_nav_menu( 'mobile' ) ) {
	$storecraft_sidebar_menu_location = 'mobile';
}

$storecraft_sidebar_currency = get_theme_mod( 'storecraft_header_language_text', __( 'USD', 'storecraft' ) );
$storecraft_account_links    = storecraft_get_sidebar_account_links();
$storecraft_social_links     = storecraft_get_sidebar_social_links();
?>

<div class="menu-modal cover-modal hbs-sidebar-menu header-footer-group" data-modal-target-string=".menu-modal">
	<div class="menu-modal-inner modal-inner">
		<div class="hbs-sidebar-panel">

			<div class="hbs-sidebar-top">
				<button type="button" class="toggle close-nav-toggle hbs-sidebar-close" data-toggle-target=".menu-modal" data-toggle-body-class="showing-menu-modal" data-set-focus=".hbs-mobile-toggle" aria-label="<?php esc_attr_e( 'Close menu', 'storecraft' ); ?>">
					<span class="screen-reader-text"><?php esc_html_e( 'Close Menu', 'storecraft' ); ?></span>
					<span class="hbs-sidebar-close-icon" aria-hidden="true">&times;</span>
				</button>

				<div class="hbs-sidebar-brand">
					<?php if ( get_theme_mod( 'storecraft_show_site_title', true ) ) : ?>
						<?php $storecraft_sidebar_name = get_bloginfo( 'name' ); ?>
						<?php if ( ! empty( $storecraft_sidebar_name ) ) : ?>
							<span class="hbs-sidebar-site-title"><?php echo esc_html( $storecraft_sidebar_name ); ?></span>
						<?php endif; ?>
					<?php endif; ?>
					<?php if ( get_theme_mod( 'storecraft_show_tagline', true ) ) : ?>
						<?php $storecraft_sidebar_tagline = get_bloginfo( 'description', 'display' ); ?>
						<?php if ( $storecraft_sidebar_tagline || is_customize_preview() ) : ?>
							<span class="hbs-sidebar-site-tagline"><?php echo esc_html( $storecraft_sidebar_tagline ); ?></span>
						<?php endif; ?>
					<?php endif; ?>
				</div>
			</div>

			<div class="hbs-sidebar-body">
				<nav class="hbs-sidebar-nav hbs-sidebar-nav-primary" aria-label="<?php echo esc_attr_x( 'Primary', 'menu', 'storecraft' ); ?>">
					<ul class="hbs-sidebar-menu-list reset-list-style">
						<?php
						if ( $storecraft_sidebar_menu_location ) {
							wp_nav_menu(
								array(
									'container'      => '',
									'items_wrap'     => '%3$s',
									'theme_location' => $storecraft_sidebar_menu_location,
									'depth'          => 0,
									'walker'         => new storecraft_Walker_Sidebar_Menu(),
									'fallback_cb'    => false,
								)
							);
						} else {
							wp_list_pages(
								array(
									'title_li'    => false,
									'depth'       => 1,
									'link_before' => '<span class="hbs-sidebar-menu-icon" aria-hidden="true"><i class="fas fa-home"></i></span><span class="hbs-sidebar-menu-text">',
									'link_after'  => '</span>',
								)
							);
						}
						?>
					</ul>
				</nav>

				<hr class="hbs-sidebar-divider" />

				<?php if ( get_theme_mod( 'storecraft_sidebar_show_account', true ) ) : ?>
				<nav class="hbs-sidebar-nav hbs-sidebar-nav-account" aria-label="<?php esc_attr_e( 'Account menu', 'storecraft' ); ?>">
					<ul class="hbs-sidebar-menu-list reset-list-style">
						<?php foreach ( $storecraft_account_links as $storecraft_account_link ) : ?>
							<li>
								<a href="<?php echo esc_url( $storecraft_account_link['url'] ); ?>">
									<span class="hbs-sidebar-menu-icon" aria-hidden="true"><i class="<?php echo esc_attr( $storecraft_account_link['icon'] ); ?>"></i></span>
									<span class="hbs-sidebar-menu-text"><?php echo esc_html( $storecraft_account_link['label'] ); ?></span>
								</a>
							</li>
						<?php endforeach; ?>
					</ul>
				</nav>

				<hr class="hbs-sidebar-divider" />
				<?php endif; ?>

				<?php if ( get_theme_mod( 'storecraft_sidebar_show_currency', true ) ) : ?>
				<div class="hbs-sidebar-currency-row">
					<span class="hbs-sidebar-currency-label">
						<i class="fas fa-globe" aria-hidden="true"></i>
						<?php esc_html_e( 'Currency', 'storecraft' ); ?>
					</span>
					<button type="button" class="hbs-sidebar-currency-value" aria-label="<?php esc_attr_e( 'Select currency', 'storecraft' ); ?>" aria-expanded="false">
						<span><?php echo esc_html( $storecraft_sidebar_currency ); ?></span>
					</button>
				</div>
				<?php endif; ?>
			</div>

			<div class="hbs-sidebar-footer">
				<ul class="hbs-sidebar-social reset-list-style">
					<?php foreach ( $storecraft_social_links as $storecraft_social_link ) : ?>
						<li>
							<?php if ( ! empty( $storecraft_social_link['url'] ) ) : ?>
								<a href="<?php echo esc_url( $storecraft_social_link['url'] ); ?>"<?php echo ( 0 === strpos( $storecraft_social_link['url'], 'http' ) ) ? ' target="_blank" rel="noopener noreferrer"' : ''; ?> aria-label="<?php echo esc_attr( $storecraft_social_link['label'] ); ?>">
									<i class="<?php echo esc_attr( $storecraft_social_link['icon'] ); ?>" aria-hidden="true"></i>
								</a>
							<?php else : ?>
								<span class="hbs-sidebar-social-icon" aria-label="<?php echo esc_attr( $storecraft_social_link['label'] ); ?>">
									<i class="<?php echo esc_attr( $storecraft_social_link['icon'] ); ?>" aria-hidden="true"></i>
								</span>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
			</div>

		</div>
	</div>
</div>
