(function(){"use strict";function b(t){return(t??"").normalize("NFKD").replace(/[\u0300-\u036f]/g,"").toLowerCase().replace(/[*:()[\]{}]/g," ").replace(/[_-]/g," ").replace(/\s+/g," ").trim()}function p(t){const e=new Map;for(const n of t){if(!n.approved||!n.suggestedValue)continue;const a=b(n.fieldLabel);a&&e.set(a,{normalizedLabel:a,fieldLabel:n.fieldLabel,profilePath:n.profilePath,suggestedValue:n.suggestedValue,approved:n.approved,timestamp:new Date().toISOString()})}return Array.from(e.values())}function y(t,e){const n=new Map(t.map(a=>[a.normalizedLabel,a]));for(const a of e)n.set(a.normalizedLabel,a);return Array.from(n.values())}const c=new Map,l="applypilot.session";async function d(){const[t]=await chrome.tabs.query({active:!0,lastFocusedWindow:!0});if(t!=null&&t.id)return t;const[e]=await chrome.tabs.query({active:!0,currentWindow:!0});if(e!=null&&e.id)return e;throw new Error("No active tab available.")}async function u(t){if(t){const e=await chrome.tabs.get(t);if(e!=null&&e.id)return e}return d()}async function f(t){return t?(await chrome.tabs.query({})).find(n=>{var a;return n.id&&((a=n.url)==null?void 0:a.includes(t))}):void 0}function S(t){return t?/^(chrome|edge|about|file):/i.test(t)||t.startsWith("chrome-extension://")||t.includes("chrome.google.com/webstore")||t.includes("chromewebstore.google.com"):!0}async function g(){const e=(await chrome.storage.session.get(l))[l];return e||{}}async function h(){const t={};for(const[e,n]of c.entries())t[String(e)]=n;await chrome.storage.session.set({[l]:t})}async function A(){if(c.size>0)return;const t=await g();for(const[e,n]of Object.entries(t))c.set(Number(e),n)}async function m(t){try{const e=await chrome.tabs.sendMessage(t,{type:"APPLYPILOT_PING"});return(e==null?void 0:e.ok)===!0}catch{return!1}}async function L(t,e=5,n=120){for(let a=0;a<e;a+=1){if(await m(t))return!0;await new Promise(r=>setTimeout(r,n))}return!1}async function w(t,e){if(S(e))throw new Error("This page does not allow extension form scanning. Use the popup's manual copy tools or open the application form itself.");if(!await m(t)){try{await chrome.scripting.executeScript({target:{tabId:t},files:["content.js"]})}catch{throw new Error("ApplyPilot could not attach to this page. Some job sites block extension injection until the form itself is open.")}if(!await L(t))throw new Error("ApplyPilot could not reach the page after injection. Refresh the application page, open the form section you want to fill, then scan again.")}}function C(t,e,n){const a=new URL(e).hostname,r=(t==null?void 0:t.host)===a;return{host:a,scanCount:r?t.scanCount+1:1,lastUrl:e,lastScanAt:new Date().toISOString(),lastAdapterName:n,approvedFieldCount:r?t.approvedFieldCount:0,rememberedCount:r?t.rememberedMatches.length:0,rememberedMatches:r?t.rememberedMatches:[]}}function M(t,e,n){const a=new URL(e).hostname,r=(t==null?void 0:t.host)===a,i=y(r?(t==null?void 0:t.rememberedMatches)??[]:[],p(n.matches));return{host:a,scanCount:(t==null?void 0:t.scanCount)??1,lastUrl:e,lastScanAt:(t==null?void 0:t.lastScanAt)??new Date().toISOString(),lastAdapterName:t==null?void 0:t.lastAdapterName,approvedFieldCount:n.matches.filter(s=>s.approved&&s.suggestedValue).length,rememberedCount:i.length,rememberedMatches:i}}chrome.runtime.onMessage.addListener((t,e,n)=>((async()=>{var a;try{if(await A(),(t==null?void 0:t.type)==="SCAN_ACTIVE_TAB"){const r=await f(t.urlMatch)??await u(t.tabId);await w(r.id,r.url);const i=await chrome.tabs.sendMessage(r.id,{type:"APPLYPILOT_SCAN"}),s=c.get(r.id),o=r.url?C(s,r.url,(a=i==null?void 0:i.scan)==null?void 0:a.adapterName):void 0;o&&(c.set(r.id,o),await h()),n({...i,session:o,rememberedMatches:(o==null?void 0:o.rememberedMatches)??[]});return}if((t==null?void 0:t.type)==="FILL_ACTIVE_TAB"){const r=await f(t.urlMatch)??await u(t.tabId);await w(r.id,r.url);const i=await chrome.tabs.sendMessage(r.id,{type:"APPLYPILOT_FILL",request:t.request}),s=c.get(r.id),o=r.url?M(s,r.url,t.request):s;r.url&&o&&(c.set(r.id,o),await h()),n({...i,session:o});return}if((t==null?void 0:t.type)==="GET_ACTIVE_SESSION"){const r=await d();n({ok:!0,session:c.get(r.id)});return}n({ok:!1,error:"Unknown message."})}catch(r){n({ok:!1,error:r instanceof Error?r.message:"Unexpected extension error."})}})(),!0))})();
//# sourceMappingURL=background.js.map
