<?php

namespace vTables\Classes;

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

/**
 * GET HASHED VUE FILES
 */
function vtables_get_hashed_file($name, $type) {
  $folder = VTABLES_PRO_DIR . 'dist/spa/assets/';
  $files = list_files($folder);
  foreach ( $files as $file ) {
    if ( is_file( $file ) && strpos(basename($file), $name) !== false ) {
      $filetype = wp_check_filetype($file);
      if ($filetype['ext'] === $type) {
        return basename($file);
      }
    }
  }
}

/**
 * LOAD ASSETS CLASS
 */
class LoadAssets {
  public function add_type_attribute( $tag, $handle, $src ) {
    if ( 'vtables-index' !== $handle ) {
      return $tag;
    }

    $tag = str_replace('<script ', '<script type="module" ', $tag);
    return $tag;
  }

  public function vtables_enqueue_assets() {
    $env_file_path = VTABLES_PRO_DIR . 'env.php';

    if (file_exists($env_file_path)) {
      include($env_file_path);
      $vtables_mode = constant('VTABLES_PRO_MODE');
    }

    if (isset($vtables_mode) && $vtables_mode === 'dev') { // dev mode

      wp_enqueue_style('vtables-material-icons', VTABLES_PRO_URL . 'public/css/material-icons-font-dist.css', false, null);
      wp_enqueue_script('vtables-script-boot', constant('VTABLES_PRO_BASE_URL') . '/.quasar/client-entry.js', array('jquery'), null, true);

    } else { // prod mode

      wp_enqueue_style( 'vtables-material-icons', VTABLES_PRO_URL . 'dist/spa/css/material-icons-font-dist.css', false, null);

      $app_css = VTABLES_PRO_URL . 'dist/spa/assets/' . vtables_get_hashed_file('index', 'css');
      wp_enqueue_style( 'vtables-index', $app_css, false, null);

      wp_localize_script(
        'wp-api',
        'WP_API_Settings',
        array(
          'nonce' => wp_create_nonce('wp_rest'),
          'base_url' => rest_url(),
        )
      );
      wp_enqueue_script('wp-api');

      $app_js = VTABLES_PRO_URL . 'dist/spa/assets/' . vtables_get_hashed_file('index', 'js');
      wp_enqueue_script( 'vtables-index', $app_js, array('jquery'), VTABLES_PRO_VERSION, false);

      add_filter( 'script_loader_tag', array( $this, 'add_type_attribute' ), 10, 3 );

    }
  }
}

