<?php

/**
 * Plugin Function file.
 *
 * @since 1.0.0
 *
 * @package LinkFiliate
 */

defined('ABSPATH') || exit;

//global
add_action('admin_enqueue_scripts', function () {
	wp_enqueue_script(
		'linkfiliate-admin-global',
		LINKFILIATE_PLUGIN_URL . '/assets/admin/global/index.js',
		['wp-element', 'wp-api-fetch'], // ensures React & wp-api-fetch are loaded
		filemtime(LINKFILIATE_PLUGIN_DIR . '/assets/admin/global/index.js'),
		true
	);

	wp_enqueue_style(
		'linkfiliate-admin-global',
		LINKFILIATE_PLUGIN_URL . '/assets/admin/global/index.css',
		[],
		filemtime(LINKFILIATE_PLUGIN_DIR . '/assets/admin/global/index.css')
	);

	// Optional if your JS needs nonce or AJAX URL
	wp_localize_script('linkfiliate-admin-global', 'linkfiliateData', [
		'ajaxUrl' => admin_url('admin-ajax.php'),
		'nonce'   => wp_create_nonce('linkfiliate_admin_nonce'),
	]);
});



add_action('admin_enqueue_scripts', function ($hook) {
	if ($hook === 'post.php' || $hook === 'post-new.php') {
		wp_enqueue_script(
			'linkfiliate-classic-button',
			LINKFILIATE_PLUGIN_URL . '/assets/admin/classic/index.js',
			['wp-element', 'wp-api-fetch'], // load React & ReactDOM
			filemtime(LINKFILIATE_PLUGIN_DIR . '/assets/admin/classic/index.js'),
			true
		);
		wp_enqueue_style('wp-components');
	}
});

add_action('mce_external_plugins', function ($plugins) {
	$plugins['classic_modal'] = LINKFILIATE_PLUGIN_URL . '/assets/admin/classic/index.js';
	return $plugins;
});

add_action('mce_buttons', function ($buttons) {
	$buttons[] = 'classic_modal';
	return $buttons;
});

if ( ! function_exists( 'linkfiliate_fs' ) ) {
    // Create a helper function for easy SDK access.
    function linkfiliate_fs() {
        global $linkfiliate_fs;

        if ( ! isset( $linkfiliate_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( LINKFILIATE_PLUGIN_FILE ) . '/vendor/freemius/start.php';

            $linkfiliate_fs = fs_dynamic_init( array(
                'id'                  => '21843',
                'slug'                => 'linkfiliate',
                'type'                => 'plugin',
                'public_key'          => 'pk_ab5bd327ac32a4e79fee15e74787d',
				'is_premium' 		  => false,
				'is_org_compliant'    => true,
				'has_premium_version' => true,
                'has_addons'          => false,
                'has_paid_plans'      => true,
				'has_affiliation'     => 'selected',
                'menu'                => array(
                    'slug'           => 'linkfiliate',
                    'account'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $linkfiliate_fs;
    }

    // Init Freemius.
    linkfiliate_fs();
    // Signal that SDK was initiated.
    do_action( 'linkfiliate_fs_loaded' );
}