import{b as m}from"./browser-polyfill-CZ_dLIqp.js";import{i as s,n as C}from"./downloadIntent-Dv31jC2S.js";const S=8e3,i=new Map;function v(){const e=Date.now();for(const[n,t]of i.entries())t<=e&&i.delete(n)}async function a(e,n){const t=C(e,window.location.href);if(!t)return!1;if(v(),i.has(t))return!0;i.set(t,Date.now()+S);try{if((await m.runtime.sendMessage({type:"capture-link-download",url:t,referer:n||document.referrer||window.location.href}))?.ok)return!0}catch{}return i.delete(t),!1}function g(e){return e?e instanceof HTMLAnchorElement?e:e instanceof Element?e.closest("a[href]"):null:null}function b(e){return!!(e.metaKey||e.ctrlKey||e.shiftKey||e.altKey)}async function k(e){if(e.defaultPrevented||b(e))return;const n=g(e.target);if(!n)return;const t=n.href||"";!t||!s(t,window.location.href)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),await a(t,document.referrer||window.location.href))}function E(e){const n=g(e.target);if(!n)return;const t=n.href||"";!t||!s(t,window.location.href)||b(e)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),a(t,document.referrer||window.location.href))}document.addEventListener("pointerdown",e=>{e.button===0&&E(e)},!0);document.addEventListener("mousedown",e=>{e.button===0&&E(e)},!0);document.addEventListener("click",e=>{e.button===0&&k(e)},!0);document.addEventListener("keydown",e=>{if(e.key!=="Enter"||e.defaultPrevented||b(e))return;const n=g(e.target);if(!n)return;const t=n.href||"";!t||!s(t,window.location.href)||(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),a(t,document.referrer||window.location.href))},!0);document.addEventListener("auxclick",e=>{e.button===1&&k(e)},!0);function L(){try{const e=window.open.bind(window);window.open=function(t,o,u){const y=String(t||"").trim();return y&&s(y,window.location.href)?(a(y,window.location.href),null):e(t,o,u)}}catch{}try{const e=HTMLAnchorElement.prototype.click;HTMLAnchorElement.prototype.click=function(){const t=this.href||this.getAttribute("href")||"";if(t&&s(t,window.location.href)){a(t,document.referrer||window.location.href);return}e.call(this)}}catch{}}L();let l=null,r=null,w=!1,x=null,d=window.location.href;function T(e){try{const n=new URL(e);return n.hostname!=="www.youtube.com"&&n.hostname!=="youtube.com"?!1:n.pathname==="/watch"&&n.searchParams.has("v")}catch{return!1}}function c(e,n="idle"){r&&(r.textContent=e,n==="ok"?r.style.color="#8ff0a4":n==="error"?r.style.color="#ff9b9b":r.style.color="#aeb7d8")}async function I(){if(!w){w=!0,c("gdown으로 전송 중...");try{const e=await m.runtime.sendMessage({type:"page:enqueue-ytdlp-url",url:window.location.href,referer:window.location.href});e?.ok?c("다운로드 모달로 전송됨","ok"):c(`전송 실패: ${e?.error||"unknown error"}`,"error")}catch(e){c(`전송 실패: ${String(e)}`,"error")}finally{w=!1}}}function M(){l&&(l.remove(),l=null,r=null)}function p(){if(window.top!==window.self)return;if(!T(window.location.href)){M();return}if(l)return;const e=document.createElement("div");e.id="gomdown-youtube-overlay",e.style.position="fixed",e.style.right="20px",e.style.bottom="24px",e.style.zIndex="2147483647",e.style.background="rgba(17, 21, 32, 0.94)",e.style.border="1px solid rgba(133, 148, 195, 0.35)",e.style.borderRadius="12px",e.style.padding="10px",e.style.boxShadow="0 8px 24px rgba(0, 0, 0, 0.28)",e.style.backdropFilter="blur(6px)",e.style.width="220px",e.style.fontFamily="ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif",e.style.color="#e8edff";const n=document.createElement("div");n.textContent="Gdown Helper",n.style.fontSize="12px",n.style.fontWeight="700",n.style.marginBottom="8px";const t=document.createElement("button");t.type="button",t.textContent="이 영상 다운로드",t.style.width="100%",t.style.height="34px",t.style.border="1px solid #5a69f0",t.style.borderRadius="8px",t.style.background="#5a69f0",t.style.color="#ffffff",t.style.fontSize="12px",t.style.fontWeight="700",t.style.cursor="pointer",t.addEventListener("click",()=>{I()});const o=document.createElement("div");o.textContent="클릭 시 gdown 다운로드 모달로 연결",o.style.fontSize="11px",o.style.marginTop="8px",o.style.lineHeight="1.35",o.style.color="#aeb7d8",e.appendChild(n),e.appendChild(t),e.appendChild(o),document.documentElement.appendChild(e),l=e,r=o}function P(){x===null&&(x=window.setInterval(()=>{const e=window.location.href;e!==d&&(d=e,p())},800),window.addEventListener("popstate",()=>{d=window.location.href,p()}),document.addEventListener("yt-navigate-finish",()=>{d=window.location.href,p()}))}p();P();let h=null,f=null;function O(){if(h)return h;const e=document.createElement("div");return e.id="gomdown-media-toast",e.style.position="fixed",e.style.left="18px",e.style.bottom="18px",e.style.zIndex="2147483647",e.style.maxWidth="360px",e.style.padding="10px 12px",e.style.borderRadius="10px",e.style.border="1px solid rgba(128, 140, 180, 0.42)",e.style.background="rgba(18, 21, 31, 0.95)",e.style.color="#dce4fa",e.style.fontSize="12px",e.style.lineHeight="1.35",e.style.fontFamily="ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif",e.style.boxShadow="0 10px 24px rgba(0, 0, 0, 0.28)",e.style.display="none",document.documentElement.appendChild(e),h=e,e}function A(e){const n=O(),t=String(e?.kind||"media").toUpperCase(),o=String(e?.suggestedOut||"").trim(),u=String(e?.url||"").trim().slice(0,96);n.textContent=o?`캡처됨 [${t}] ${o}`:`캡처됨 [${t}] ${u}${u.length>=96?"…":""}`,n.style.display="block",f!==null&&window.clearTimeout(f),f=window.setTimeout(()=>{n.style.display="none",f=null},2200)}m.runtime.onMessage.addListener(e=>{e?.type==="media:captured"&&A({kind:e?.kind,url:e?.url,suggestedOut:e?.suggestedOut})});