<?php

/**
 * 
 */
function dragblock_enqueue_register($handle, $path, $dep = null)
{
    // enqueue script
    if (strpos($path, '.js')) {
        wp_register_script(
            $handle,
            DRAGBLOCK_URL  . $path,
            $dep,
            DRAGBLOCK_VERSION,
            true
        );
    }
    // enqueue style
    else {
        wp_register_style(
            $handle,
            DRAGBLOCK_URL  . $path,
            $dep,
            DRAGBLOCK_VERSION
        );
    }
}
/**
 * @param {string} $handle id of the script/style
 * @param {string} $path full path to the file, including the extension
 */
function dragblock_enqueue($handle, $path, $dep = null)
{    
    dragblock_enqueue_register($handle, $path, $dep);
    // enqueue script
    if (strpos($path, '.js')) {
        wp_enqueue_script($handle);
    }
    // enqueue style
    else {
        wp_enqueue_style($handle);
    }
}


/**
 * Enqueue both css and js with dependencies if necessary for admin page applications
 * 
 * @param {string} $path full path file, including the file name and extension 
 */
function dragblock_admin_page_enqueue($path, $dep = array())
{
	// Load the required WordPress packages.
	// Automatically load imported dependencies and assets version.
	$asset_file = include $path . 'index.asset.php';

	// Enqueue CSS dependencies of the scripts included in the build.
	foreach ($asset_file['dependencies'] as $style) {
		wp_enqueue_style($style);
	}

	foreach ($asset_file['dependencies'] as $script) {
		array_push($dep, $script);
	}
	// always support translation files
	array_push($asset_file['dependencies'], 'wp-i18n');
	
	$raw_path = str_replace(DRAGBLOCK_PATH, '', $path);

	dragblock_enqueue($path, $raw_path . 'index.css', $dep);
	dragblock_enqueue($path, $raw_path . 'index.js', $dep);
}
