	
	import('https://cdn.jsdelivr.net/npm/@cdek-it/widget@3')
	.then(module => {

	let cdek;
	let ready = false;

	const map = document.getElementById('map');

	function init(location) {

		cdek = new window.CDEKWidget({ 
							from: {
								code: 248,
								address: 'Екатерининская 116'
							}, 
							root: 'map', 
							apiKey: 'a222c5fe-fee7-420a-b979-aec0473a0400', 
							servicePath: 'https://arming.dev.mirzaev.sexy/api/cdek', 
              hideFilters: {
								have_cashless: true,
                have_cash: true,
                is_dressing_room: true,
                type: false,
              },
							hideDeliveryOptions: {
								office: false,
                door: false,
              },
							lang: '{{ language.name == "ru" ? "rus" : "eng" }}',
							currency: '{{ currency.name|upper }}',
							tariffs: {
								office: [234, 136, 185, 368, 378, 498, 2323, 2360, 510, 291],
                door: [233, 137, 186, 2322, 294],
              },
							defaultLocation: [location?.longitude || {{ session.buffer.delivery[session.buffer.delivery.company].longitude ?? session.buffer.delivery[session.buffer.delivery.company].location.data.longitude ?? 37.622513 }}, location?.latitude || {{ session.buffer.delivery[session.buffer.delivery.company].latitude ?? session.buffer.delivery[session.buffer.delivery.company].location.data.latitude ?? 55.75322 }}],
              onReady() {
								// map?.style.setProperty('--animation-end-height', '240px');
								// map?.classList.add('animated', 'slide-down');
							},
              onCalculate(tariffs, address) {
									// Deleting the delivery cost <span> element
									//document.getElementById("cost_delivery")?.remove();

									// Deleting the delivery days <span> element
									//document.getElementById("shipping")?.remove();

									// Disablind the order button
									//document.getElementById("order")?.setAttribute('disabled', true);

								//
								// map.style.setProperty('height', '300px');
              },
              onChoose(type, tariff, address) {
								//
								// map.style.removeProperty('height');

								//
								const apartament = document.getElementById('apartament');

								if (type === 'door') apartament.classList.remove('hidden');
								else apartament.classList.add('hidden'); 

									// Deleting the delivery cost <span> element
									document.getElementById("cost_delivery")?.remove();

									// Deleting the delivery days <span> element
									document.getElementById("shipping")?.remove();

									// Disablind the order button
									document.getElementById("order")?.setAttribute('disabled', true);

								core.modules.connect("damper").then(
									() => {
										// Imported the damper module

										// Execute under damper
										if (type)
											core.delivery.write.damper("type", type, true);

										// Execute under damper
										if (tariff)
											core.delivery.write.damper("tariff", tariff.tariff_code, true);

										// Execute under damper
										if (address.location) {
											core.delivery.write.damper("longitude", address.location[0], true);
											core.delivery.write.damper("latitude", address.location[1], true);
										} else if (address.position) {
										console.log(address.position);
											core.delivery.write.damper("longitude", address.position[0], true);
											core.delivery.write.damper("latitude", address.position[1], true);
										}
									},
									() => {
										// Not imported the damper module

										// Execute
										if (type)
											core.delivery.write.system("type", type, true);

										// Execute
										if (tariff)
											core.delivery.write.system("tariff", tariff.tariff_code, true);

										// Execute
										if (address.location) {
											core.delivery.write.system("longitude", address.location[0], true);
											core.delivery.write.system("latitude", address.location[1], true);
										} else if (address.position) {
											core.delivery.write.system("longitude", address.position[0], true);
											core.delivery.write.system("latitude", address.position[1], true);
										}
									},
								);
								//
								const location = document.getElementById('location');

								if (location instanceof HTMLInputElement) {
									//
									/**
									// Declaring location value buffer
									let value = address.city;

									// Declaring variables for structure of the address
									let country, region, sub_region;

									if (type === 'office') {
										// Office

										//
										region = address.region;
									} else {
										// Door

										// Initializing components of the address
										const components = address.components.reverse();

										//
										country = components[0].name;
										region = components[1].name;
										sub_region = components[2].name;
									}

									if (region && region !== address.city)
										value += ", " + region;
									if (sub_region && sub_region !== region && sub_region !== address.city)
										value += ", " + sub_region;
									if (country)
										value += ", " + country;

									//
									location.value = value;
									*/

									location.value = address.city;
								
									//
									core.delivery.location(location, true, false, false, true);
								}

								//
								const street = document.getElementById('street');

								if (type === 'office') {
									// Office

									if (street instanceof HTMLInputElement) {
										//

										//
										street.value = address.address;

										//
										core.delivery.street(street, true, false, false);
									}
								} else {
									// Door

									if (street instanceof HTMLInputElement) {
										//

										//
										street.value = address.name;

										//
										core.delivery.street(street, true, false, false);
									}
								}

								// Initializing the delivery data request <section> element
								const request = document.getElementById("delivery_request");

						if (request instanceof HTMLElement) {
							// Initializeg the delivery data request <section> element

							// Hiding the delivery data request <section> element
							request.classList.add("hidden");
							}
              },
						});

					{% for product in formatted %}
						cdek?.addParcel({
							weight: Math.ceil({{ product.weight is empty ? 0 : product.weight }}),
              width: Math.ceil({{ product.x is empty ? 0 : product.x }}),
              height: Math.ceil({{ product.y is empty ? 0 : product.y }}),
							length: Math.ceil({{ product.z is empty ? 0 : product.z }})
            });
					{% endfor %}
					}


					document.addEventListener('core.telegram.api.location.received', (event) => {
						if (!ready) {
							ready = true;
							init(event.detail.location);
						}
					});

					document.addEventListener('core.telegram.api.location.rejected', (event) => {
						if (!ready) {
							ready = true;
							init();
						}
					});

					document.addEventListener('core.delivery.calculated', (event) => {
						if (event.detail.map && event.detail.json.longitude && event.detail.json.latitude)
							cdek?.updateLocation([event.detail.json.longitude, event.detail.json.latitude]);
					});

					document.addEventListener('core.cart.summary.received', (event) => {
						cdek?.resetParcels();

						for (const product of event.detail.json?.products) {
							cdek?.addParcel({
								weight: Math.ceil(product.weight || 0),
								width: Math.ceil(product.x || 0),
								height: Math.ceil(product.y || 0),
								length: Math.ceil(product.z || 0)
							});
						}
					});

					setTimeout(() => 
					{
					if (!ready) {
							ready = true;
							init();
						}
					}, 5000);
	});
