jQuery(document).ready(function ($) {
	function toggleSubscriptionTab() {
		const isSubscriptionChecked = $('#_is_subscription').is(':checked');
		const subscriptionTab = $('.subscription_settings_tab');
		const subscriptionPanel = $('#subscription_settings_data');

		if (isSubscriptionChecked) {
			subscriptionTab.show();
			subscriptionPanel.addClass('active');
			subscriptionPanel.css('display', 'block');
		} else {
			subscriptionTab.hide();
			subscriptionPanel.removeClass('active');
			subscriptionPanel.css('display', 'none');
		}
	}
	toggleSubscriptionTab();
	$('#_is_subscription').on('change', toggleSubscriptionTab);
	const $perDownloadField = $('#_subscription_per_download');
	const $billingPeriodField = $('#_subscription_billing_period');
	const $perDownloadContainer = $perDownloadField.closest('p');
	const $billingPeriodContainer = $billingPeriodField.closest('p');

	function toggleFields() {
		const perDownloadValue = $perDownloadField.val();
		const billingPeriodValue = $billingPeriodField.val();

		if (perDownloadValue && perDownloadValue.trim() !== '') {
		} else {
			$billingPeriodContainer.show();
		}

		if (billingPeriodValue && billingPeriodValue.trim() !== '') {
			$perDownloadContainer.hide();
		} else {
			$perDownloadContainer.show();
		}
	}
	$perDownloadField.on('input', toggleFields);
	$billingPeriodField.on('input', toggleFields);
	toggleFields();

	function setupCopyButton(buttonId, textId, messageId) {
		const button = document.getElementById(buttonId);
		if (button) {
			button.addEventListener('click', function () {
				const copyText = document.getElementById(textId);
				if (copyText) {
					copyText.select();
					document.execCommand('copy');
					const copyMessage = document.getElementById(messageId);
					if (copyMessage) {
						copyMessage.style.display = 'block';
						setTimeout(function () {
							copyMessage.style.display = 'none';
						}, 1500);
					}
				}
			});
		}
	}
	setupCopyButton(
		'bwd-login-shortcode-copy',
		'hidden-login-copy-text',
		'copy-login-message'
	);
	setupCopyButton('bwd-shortcode-copy', 'hidden-copy-text', 'copy-message');
	setupCopyButton(
		'bwd-shortcode-subscription',
		'hidden-subscription-text',
		'subscription-message'
	);
	setupCopyButton(
		'bwd-shortcode-dashboard',
		'hidden-dashboard-text',
		'dashboard-message'
	);
	setupCopyButton(
		'bwd-shortcode-instant-cv-create',
		'hidden-instant-cv-create-text',
		'instant-cv-create-message'
	);
});