!function(){"use strict";var n={payload:null,androidTimer:null};function e(n){return document.querySelector(n)}function t(n,t){var o=e(n);o&&o.classList.toggle("d-none",!t)}function o(n,t){var o=e(n);o&&(o.textContent=t||"")}function i(n,t){var o=e("#mobile-connect-status");o&&(o.textContent=n,o.className="alert small mb-3 alert-"+(t||"light"),"light"===t&&o.classList.add("border"))}function c(){return/Android/i.test(navigator.userAgent||"")}function a(){window.EasyChatModal&&"function"==typeof window.EasyChatModal.show&&window.EasyChatModal.show("#mobileConnectModal")}function r(n,e){return(e=e||{}).credentials="same-origin",e.headers=Object.assign({Accept:"application/json"},e.headers||{}),fetch(function(n){if(window.EasyChatRequest&&"function"==typeof window.EasyChatRequest.routeUrl)return window.EasyChatRequest.routeUrl(n);if("function"==typeof window.EasyChatRouteUrl)return window.EasyChatRouteUrl(n);var e=String(n||""),t="index.php?route=",o=String(window.EasyChatRouteBase||"");return o&&0===e.indexOf(t)?o+e.substring(16):e}(n),e).then(function(n){return n.json().catch(function(){return{}}).then(function(e){return e.http_status=n.status,e})})}function l(n,t){var o=e(n);if(o){if(!window.qrcode)return o.removeAttribute("src"),void(o.alt="QR generator is unavailable");var i=window.qrcode(0,"M");i.addData(t),i.make(),o.src=i.createDataURL(7,12)}}function d(n){try{var e=new URL(n,window.location.href);return new URLSearchParams(e.hash.replace(/^#/,"")).get("p")||""}catch(n){return""}}function u(n){var e=d(n.connect_url||"");return e?"easychat://connect#p="+encodeURIComponent(e):""}function s(n){var e=d(n.connect_url||"");return e?"intent://connect/#Intent;scheme=easychat;package="+(n.android_package||"ru.liveopencart.easychat")+";S.easychat_payload="+encodeURIComponent(e)+";S.browser_fallback_url="+encodeURIComponent(n.connect_url)+";end":""}function f(n){var e=document.createElement("textarea");e.value=n,e.setAttribute("readonly","readonly"),e.style.position="fixed",e.style.left="-9999px",document.body.appendChild(e),e.select();try{document.execCommand("copy")}catch(n){}document.body.removeChild(e)}function m(e){e&&e.connect_url&&(i("Открываем EasyChat. Если приложение не открылось, нажмите «Открыть приложение» еще раз или отсканируйте QR.","info"),window.clearTimeout(n.androidTimer),n.androidTimer=window.setTimeout(function(){i("Если приложение не открылось, обновите EasyChat до последней версии из RuStore и отсканируйте QR из этой панели.","warning")},2400),window.location.href=s(e)||u(e)||e.connect_url)}function p(){return a(),t("#mobile-connect-android-view",!0),t("#mobile-connect-ios-view",!1),t("#mobile-connect-open-app",!1),i("Готовим одноразовую ссылку...","light"),(d=e("#easychat-mobile-connect-toggle"),u=d&&d.getAttribute("data-mobile-connect-nonce")||"",r("index.php?route=mobile_connect/create",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({nonce:u})})).then(function(a){if(!a||!a.success||!a.connect_url)throw new Error("pairing_failed");!function(a){n.payload=a,t("#mobile-connect-android-view",!0),t("#mobile-connect-ios-view",!0),t("#mobile-connect-open-app",c()),o("#mobile-connect-site-url",a.site_url||""),l("#mobile-connect-qr-img",a.connect_url);var r=e("#mobile-connect-rustore-link");r&&a.android_store_url&&(r.href=a.android_store_url),i("Ссылка готова. Она действует 10 минут и погаснет после первого входа.","success")}(a),c()&&(t("#mobile-connect-ios-view",!1),m(a))}).catch(function(){i("Не удалось подготовить подключение. Обновите страницу и попробуйте еще раз.","danger")});var d,u}function h(){a(),t("#mobile-connect-android-view",!1),t("#mobile-connect-ios-view",!0),i("На iPhone установите EasyChat как веб-приложение на домашний экран, затем разрешите уведомления.","info"),l("#mobile-connect-ios-qr-img",window.location.href.split("#")[0])}function w(){var n;(n=navigator.userAgent||"",/iPad|iPhone|iPod/.test(n)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1)?h():p()}document.addEventListener("DOMContentLoaded",function(){var i=e("#easychat-mobile-connect-toggle");i&&i.addEventListener("click",function(n){n.preventDefault(),w()});var c=e("#mobile-connect-copy-link");c&&c.addEventListener("click",function(){var e,t,i;n.payload&&n.payload.connect_url&&(e=n.payload.connect_url,i=function(){o(t||"#mobile-connect-copy-feedback","Скопировано"),window.setTimeout(function(){o(t||"#mobile-connect-copy-feedback","")},1800)},navigator.clipboard&&window.isSecureContext?navigator.clipboard.writeText(e).then(i).catch(function(){f(e),i()}):(f(e),i()))});var a=e("#mobile-connect-open-app");a&&a.addEventListener("click",function(){m(n.payload)});var r=e("#mobile-connect-ios-qr");r&&r.addEventListener("click",function(){t("#mobile-connect-ios-qr-wrap",!0),l("#mobile-connect-ios-qr-img",window.location.href.split("#")[0])})}),window.EasyChatMobileConnect={open:w,openAndroid:p,openIosGuide:h,androidIntentUrl:s,customSchemeUrl:u}}();