<?php
/**
 * Easy Populate Posts assets.
 *
 * @package spp
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$dir  = SPP_PLUGIN_DIR . 'build/';
$url  = SPP_PLUGIN_URL . 'build/';
$deps = [
	'dependencies' => [],
	'version'      => time(),
];

if ( file_exists( $dir . 'index.asset.php' ) ) {
	$deps = require $dir . 'index.asset.php';
}

if ( file_exists( $dir . 'index.js' ) && ! wp_script_is( 'spp-custom' ) ) {
	wp_register_script( 'spp-custom', $url . 'index.js', $deps['dependencies'], $deps['version'], true );
	wp_localize_script( 'spp-custom', 'sppSettings', [
		'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
		'beginImages'  => self::spp_images_mention(),
		'discardGroup' => __( 'Discard this group of settings?', 'easy-populate-posts' ),
		'messages'     => [
			'settings' => [
				'init'  => __( 'Saving settings…', 'easy-populate-posts' ),
				'done'  => __( 'Done!', 'easy-populate-posts' ),
				'ready' => __( 'Save Settings', 'easy-populate-posts' ),
			],
			'populate' => [
				'init'  => __( 'Generating posts…', 'easy-populate-posts' ),
				'done'  => __( 'Done!', 'easy-populate-posts' ),
				'ready' => __( 'Generate Posts', 'easy-populate-posts' ),
			],
		],
	] );
	wp_enqueue_script( 'spp-custom' );
}

if ( file_exists( $dir . 'style-index.css' ) && ! wp_style_is( 'spp-custom' ) ) {
	wp_enqueue_style( 'spp-custom', $url . 'style-index.css', [], $deps['version'], false );
}
