<?php
/**
 * OrokuBlocks - Router Functions
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 */

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

final class OrokuBlocks_Router {
  /**
   * Prevent more than one instance from being created
   * 
   * @since 1.0.0
   * 
   * @access private
   */
  private function __construct() {}

  /**
   * Prevent cloning
   * 
   * @since 1.0.0
   * 
   * @access private
   */
  private function __clone() {}

  /**
   * Returns admin submenu page path by submenu item id
   * 
   * @since 1.0.0
   * 
   * @param string $submenu_item_id                 Id of the admin submenu item to get path of.
   * @return string $submenu_item_page_path         Path of the admin submenu item page.
   */
  public static function adminSubmenuPagePathGet( $submenu_item_id ) {
    $submenu_item_page_path = 'orokublocks#tab=' . $submenu_item_id;

    return $submenu_item_page_path;
  }

  /**
   * Return widget path id for provided widget id
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id           Id of the widget to get path id of.
   * @return string $widget_path_id      Widget id used for file paths. 
   */
  public static function widgetPathIdGet( $widget_id ) {
    $widget_path_id = str_replace( '_', '', $widget_id );

    return $widget_path_id;
  }

  /**
   * Return widget classname for provided widget id
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id              Id of the widget to get classname of.
   * @param bool    $plugin                 Plugin to get widget classname of. Optional.
   * @return string $widget_classname       Widget classname. 
   */
  public static function widgetClassNameGet( $widget_id, $plugin = '' ) {
    $widget_name_parts = array_map(
      function ( $widget_id_part ) {
        return ucfirst( $widget_id_part );
      },
      explode( '_', $widget_id )
    );

    $widget_name = implode( '', $widget_name_parts );

    $widget_classname = 'OrokuBlocks_' . $widget_name . '_Widget';

    if ( $plugin === 'elementor' ) {
      $widget_classname = 'OrokuBlocks_' . $widget_name . '_Elementor_Widget';
    }

    return $widget_classname;
  }

  /**
   * Return base route for provided widget
   * 
   * @since 1.0.0
   * 
   * @param string  $route_type         Route type. One of: 'path', 'url'.
   * @param string  $widget_id          Id of the widget to get base route for.
   * @return string $base_route         Base route.
   * 
   * @access private
   */
  private static function widgetBaseRouteGet( $route_type, $widget_id ) {
    $base_route = orokublocks()->$route_type;

    $widget = orokublocks()->widgets[$widget_id];

    if ( orokublocks()->isProVersionValid() && ( ! orokublocks()->widgetHasFreeVersion( $widget_id ) ) ) {
      if ( array_key_exists( 'required_pro_version', $widget ) && ( orokublocks_pro()->version < $widget['required_pro_version'] ) ) {
        return $base_route;
      }

      $base_route = orokublocks_pro()->$route_type;
    }

    return $base_route;
  }

  /**
   * Return base path for provided widget
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id          Id of the widget to get base path for.
   * @return string $base_path          Base path.
   */
  public static function widgetBasePathGet( $widget_id ) {
    $base_path = self::widgetBaseRouteGet( 'path', $widget_id );

    return $base_path . '/widgets/';
  }

  /**
   * Return base url for provided widget
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id          Id of the widget to get base url for.
   * @return string $base_url           Base url.
   */
  public static function widgetBaseUrlGet( $widget_id ) {
    $base_url = self::widgetBaseRouteGet( 'url', $widget_id );

    return $base_url . 'widgets/';
  }

  /**
   * Return path for provided widget
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id          Id of the widget to get path for.
   * @return string $path               Path.
   */
  public static function widgetPathGet( $widget_id ) {
    $widget_path_id = self::widgetPathIdGet( $widget_id );
    $base_path = self::widgetBasePathGet( $widget_id );

    return $base_path . $widget_path_id;
  }

  /**
   * Return url for provided widget
   * 
   * @since 1.0.0
   * 
   * @param string  $widget_id          Id of the widget to get url for.
   * @return string $url                Url.
   */
  public static function widgetUrlGet( $widget_id ) {
    $widget_path_id = self::widgetPathIdGet( $widget_id );
    $base_url = self::widgetBaseUrlGet( $widget_id );

    return $base_url . $widget_path_id;
  }
}