"use strict";

core.modules.connect(["telegram"])
	.then(() => {
		// Imported the telegram module

		// Expanding the "Web App" window
		core.telegram.api.expand();

		// Writing settings for the "Web App" window
		core.telegram.api.enableVerticalSwipes();
		core.telegram.api.disableClosingConfirmation();
		core.telegram.api.setHeaderColor(core.theme?.getPropertyValue('--interface-top-background-color') || core.telegram.api.themeParams.header_bg_color);
		core.telegram.api.setBackgroundColor(core.theme?.getPropertyValue('--interface-background-color') || core.telegram.api.themeParams.secondary_bg_color);
		core.telegram.api.setBottomBarColor(core.theme?.getPropertyValue('--interface-bottom-background-color') || core.telegram.api.themeParams.bottom_bar_bg_color);

		// Writing settings for the "Back Button" of the "Web App" window
		core.telegram.api.BackButton.hide();

		if (core.telegram.api.themeParams.section_bg_color === core.telegram.api.themeParams.bg_color) {
			// The sections color matches the background color
			
			// Replacing the sections color with the secondary background color
			document.documentElement.style.setProperty('--tg-theme-section-bg-color', 'var(--tg-theme-secondary-bg-color)');
		}

		if (core.telegram.back.length > 0) {
			// Initialized BackButton events

			// Initializing the "Back Button" of the "Web App" window
			core.telegram.api.BackButton.show();
	
			for (const event of core.telegram.back) {
				// Iterating over BackButton events

				// Initializing the BackButton event listener
				core.telegram.api.BackButton.onClick(event);
			}
		}

		//
		core.telegram.api.ready();
	});

