<?php

/**
EDITOR
 */
add_action('enqueue_block_editor_assets', 'dragblock_interactions_panel_editor_assets');
function dragblock_interactions_panel_editor_assets()
{
	$panel = 'interactions';
	dragblock_enqueue("dragblock-{$panel}-panel", "build/applications/editor-panel-{$panel}/client/index.js", array(DRAGBLOCK_EDITOR_INIT_SLUG));
	dragblock_enqueue("dragblock-{$panel}-panel", "build/applications/editor-panel-{$panel}/client/index.css");
}

/**
We decided to place parent JS before children JS
so children could override the parent JS in some situations
 */
global $dragblock_js;
$dragblock_js = array();


/**
 * @var string $block_content the entire block html including itself's tag
 * @var array $parsed_block the block data saved in editor, ex: attributes, blockName...
 * @var WP_Block $block the block PHP object
 */
add_filter('render_block', 'dragblock_interactions_collect_js', 10, 2);
function dragblock_interactions_collect_js($block_content, $parsed_block)
{

	// var_dump('dragblock_interactions_collect_js()', $parsed_block);


	if (
		'core/null' === $parsed_block['blockName'] ||
		empty($parsed_block['attrs']['dragBlockClientId'])
	) {
		return $block_content;
	}


	/**     
	 * We added $parsed_block['blockName'] as a prefix because some block like core/post-template
	 * will have a clone as core/null which have the same attribute but will not be rendered
	 */
	// $uid_key = $parsed_block['blockName'] . $parsed_block['attrs']['dragBlockClientId'];
	$uid_key = dragblock_uid_key($parsed_block);
	$js = '';

	if (!empty($parsed_block['attrs']['dragBlockJS'])) {
		global $dragblock_uids;
		if (!empty($dragblock_uids[$uid_key])) {
			$uid_selector = '.' . $dragblock_uids[$uid_key];
			$js =
				str_replace(
					[
						'[data-dragblock-client-id="' . $parsed_block['attrs']['dragBlockClientId'] . '"]',
						'[data-dragblock-client-id=\\"' . $parsed_block['attrs']['dragBlockClientId'] . '\\"]'
					],
					$uid_selector,
					$parsed_block['attrs']['dragBlockJS']
				);
		}
	}

	// even when the js is empty
	// we still need to carry the children js    
	global $dragblock_js;


	// rearrange the css order, so the children css will be rendered after the parent css
	if (!empty($parsed_block['innerBlocks'])) {
		foreach ($parsed_block['innerBlocks'] as $inner_block) {
			if (empty($inner_block['blockName']) || empty($inner_block['attrs']['dragBlockClientId'])) {
				continue;
			}
			$key = $inner_block['blockName'] . $inner_block['attrs']['dragBlockClientId'];
			if (!empty($dragblock_js[$key])) {
				$js .= $dragblock_js[$key];
				unset($dragblock_js[$key]);
			}
		}
	}

	if ($js) {
		$dragblock_js[$uid_key] = $js;
	}

	return $block_content;
}

/**
 * FRONT END
 * collect all scripts for front-end interaction
 */
add_action('wp_footer', 'dragblock_enqueue_front_end');
function dragblock_enqueue_front_end()
{
	global $dragblock_js;

	$dragblock_save_js = implode('', $dragblock_js);

	if ($dragblock_save_js) {
		// $allowed_tags = array(
		//     'script' => array(
		//         'type' => true,
		//     ),
		// );
		// echo wp_kses("<script type='text/javascript'>{$dragblock_save_js}</script>", $allowed_tags);

		wp_add_inline_script(DRAGBLOCK_EDITOR_INIT_SLUG, $dragblock_save_js, 'before');
	}
}
