1import{j as e,r as x,R as S}from"./animation-CddyXVCz.js";import{D as q,S as H,X as te,B as D,u as X,m as K,F as V,L as Z,H as re,a3 as ae,bv as Q,bj as se,bi as oe,bw as ne,o as ie,aA as le,aE as de,bh as ce,bx as ue,by as me,h as A}from"./index-DnWsV7cE.js";import{c as xe}from"./clipboard-C0L7pyCw.js";import{b as pe}from"./resumeFilename-4GdoQ0e6.js";import{P as ge}from"./PageWrapper-C3-XQErc.js";import{u as fe,W as he}from"./WriteBackOfferCard-CBXB7DUr.js";import{H as be}from"./HandoffCard--ky8y7_b.js";import{d as ye,P as ve}from"./PersonalizedEmptyState-FC1ZItez.js";import{s as je}from"./handoffSuggestions-raELPsOv.js";import{d as we}from"./writeBackOffers-CZMm9dHV.js";import"./react-vendor-CbN3pvl2.js";import"./supabase-iKSPld20.js";import"./docgen-Bp8WKBAE.js";import"./query-DPqpbKNH.js";import"./billingLimits-4ln6JCGI.js";const Ne=({isOpen:t,onClose:s})=>{const r=q();if(!t)return null;const c=()=>{r("/auth",{state:{mode:"signup"}})};return e.jsx("div",{className:"fixed inset-0 z-[60] flex items-center justify-center bg-slate-950/70 p-4 backdrop-blur-sm",children:e.jsxs("div",{className:"w-full max-w-md overflow-hidden rounded-3xl border border-white/10 bg-white shadow-2xl dark:bg-slate-900",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 border-b border-slate-200 px-6 py-5 dark:border-slate-800",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"flex h-11 w-11 items-center justify-center rounded-full bg-emerald-100 text-emerald-700 dark:bg-emerald-950/60 dark:text-emerald-300",children:e.jsx(H,{className:"h-5 w-5"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold text-slate-900 dark:text-white",children:"Create a free account to copy and save your tailored resume"}),e.jsx("p",{className:"text-sm text-slate-500 dark:text-slate-400",children:"Guests can preview the result, but export actions are reserved for accounts."})]})]}),e.jsx("button",{type:"button",onClick:s,className:"rounded-full p-2 text-slate-400 transition-colors hover:bg-slate-100 hover:text-slate-700 dark:hover:bg-slate-800 dark:hover:text-white","aria-label":"Close export gate",children:e.jsx(te,{className:"h-5 w-5"})})]}),e.jsxs("div",{className:"space-y-4 px-6 py-5",children:[e.jsx("p",{className:"text-sm leading-6 text-slate-600 dark:text-slate-300",children:"Create your free account to keep this tailored resume, copy it to your clipboard, and download DOCX or PDF files later."}),e.jsxs("div",{className:"flex flex-col-reverse gap-3 sm:flex-row sm:items-center",children:[e.jsx(D,{variant:"ghost",size:"md",onClick:s,className:"rounded-xl border border-slate-200 text-slate-700 hover:bg-slate-50 dark:border-slate-700 dark:text-slate-200 dark:hover:bg-slate-800",children:"Maybe later"}),e.jsx(D,{variant:"primary",size:"md",onClick:c,className:"rounded-xl bg-emerald-600 text-white hover:bg-emerald-500",children:"Create Free Account"})]})]})]})})},Ce="careerdiary_cover_letter_quota",E=(t=new Date)=>`${t.getUTCFullYear()}-${String(t.getUTCMonth()+1).padStart(2,"0")}-${String(t.getUTCDate()).padStart(2,"0")}`,ee=t=>`${Ce}:${t||"guest"}`,W=(t=new Date)=>{const s=Date.UTC(t.getUTCFullYear(),t.getUTCMonth(),t.getUTCDate(),0,0,0,0);return new Date(s+1440*60*1e3)},ke=t=>{if(typeof window>"u")return{used:0,day:E()};try{const s=window.localStorage.getItem(ee(t));if(!s)return{used:0,day:E()};const r=JSON.parse(s);return!r.day||r.day!==E()?{used:0,day:E()}:{used:Number(r.used||0),day:r.day}}catch{return{used:0,day:E()}}},_e=(t,s)=>{if(!(typeof window>"u"))try{window.localStorage.setItem(ee(t),JSON.stringify({used:s,day:E()}))}catch{}},G=(t,s)=>{if(t==="paid")return{used:0,limit:Number.POSITIVE_INFINITY,remaining:Number.POSITIVE_INFINITY,resetAt:W()};const r=t==="signed_in"?1:0,c=ke(s),n=Math.min(c.used,r);return{used:n,limit:r,remaining:Math.max(r-n,0),resetAt:W()}},Se=(t,s)=>{if(t==="paid")return G(t,s);const r=G(t,s);if(r.remaining<=0)return r;const c=r.used+1;return _e(s,c),{...r,used:c,remaining:Math.max(r.limit-c,0)}},J=t=>{const s=Math.max(1,Math.ceil((t.getTime()-Date.now())/36e5));return`~${s} hour${s===1?"":"s"}`},z=({onClick:t,children:s,disabled:r=!1})=>e.jsx(D,{onClick:t,disabled:r,variant:"ghost",className:"flex items-center space-x-2 iconic-surface-soft hover:bg-white dark:hover:bg-slate-700 text-[var(--text-secondary)] font-semibold py-3 px-5 rounded-xl transition-all disabled:opacity-50 disabled:cursor-not-allowed shadow-sm hover:shadow-md active:scale-95 h-auto border-none",children:s}),De=()=>{var y,I;const{state:t,actions:s,dispatch:r,toast:c}=X(),{session:n,isGuest:N}=K(),{usage:o}=V(),{isGeneratingCoverLetter:u,coverLetter:i,profile:g,jobDescription:v}=t,{handleGenerateCoverLetter:f}=s,[M,F]=x.useState(!1),[U,j]=x.useState(!1),[d,w]=x.useState(!1),[P,T]=x.useState(null),R=x.useRef(null),C=x.useMemo(()=>{var l;return G(o.tier,(l=n==null?void 0:n.user)==null?void 0:l.id)},[(y=n==null?void 0:n.user)==null?void 0:y.id,o.tier]),h=o.tier==="signed_in"&&
1C.remaining<=0,p=o.tier==="anon";x.useEffect(()=>{var _;const l=R.current;u||(!l&&i&&o.tier==="signed_in"&&Se(o.tier,(_=n==null?void 0:n.user)==null?void 0:_.id),R.current=i||null)},[i,u,(I=n==null?void 0:n.user)==null?void 0:I.id,o.tier]),x.useEffect(()=>{if(p){w(!0),T("Create a free account to generate 1 cover letter per day.");return}if(h){w(!0),T(`Youâve used your 1 free cover letter today. Free credits reset in ${J(C.resetAt)}.`);return}w(!1),T(null)},[h,p,C.resetAt]);const k=async()=>{if(p){j(!0);return}if(h){c.error("Cover letter limit reached",`Free credits reset in ${J(C.resetAt)}.`);return}await f()},b=()=>{const l=((g==null?void 0:g.fullName)||"").trim().split(/\s+/),_=l[0]||null,a=l.length>1?l[l.length-1]:null,{companyName:m}=ne(v||"");return pe(_,a,m||null,null).replace(/_Resume$/,"")+"_CoverLetter"},O=async()=>{if(N){j(!0);return}try{await xe(i),F(!0),setTimeout(()=>F(!1),2e3)}catch{}},B=()=>{if(N){j(!0);return}se(i,`${b()}.docx`,g||void 0,l=>{c.error("Download failed",l)})},$=()=>{if(N){j(!0);return}oe(i,`${b()}.pdf`,g||void 0,void 0,l=>{c.error("Download failed",l)})};return u?e.jsxs("div",{className:"flex-grow flex flex-col items-center justify-center text-center py-10",children:[e.jsx(Z,{}),e.jsx("p",{className:"mt-4 text-lg font-semibold text-gray-700 dark:text-slate-200",children:"AI is drafting your cover letter..."}),e.jsx("p",{className:"text-sm text-gray-500 dark:text-slate-400",children:"This may take a moment."})]}):i?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex-grow mb-4",children:e.jsx("textarea",{value:i,onChange:l=>r({type:"SET_COVER_LETTER",payload:l.target.value}),className:"w-full h-full min-h-[400px] p-4 border border-[var(--border-default)] bg-transparent text-[var(--text-secondary)] rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors resize-none leading-relaxed",style:{fontFamily:"system-ui, sans-serif",fontSize:"14px",lineHeight:"1.6"}})}),e.jsxs("div",{className:"flex flex-wrap gap-3",children:[e.jsxs(z,{onClick:O,children:[e.jsx(ae,{className:"h-5 w-5"}),e.jsx("span",{children:M?"Copied!":"Copy Text"})]}),e.jsxs(z,{onClick:B,children:[e.jsx(Q,{className:"h-5 w-5"}),e.jsx("span",{children:"Download .docx"})]}),e.jsxs(z,{onClick:$,children:[e.jsx(Q,{className:"h-5 w-5"}),e.jsx("span",{children:"Download .pdf"})]})]}),e.jsx(Ne,{isOpen:U,onClose:()=>j(!1)})]}):e.jsxs("div",{className:"space-y-4",children:[d&&P&&e.jsx("div",{className:"rounded-2xl border border-indigo-200 bg-indigo-50 px-4 py-3 text-sm text-indigo-900 dark:border-indigo-900/40 dark:bg-indigo-950/30 dark:text-indigo-100",children:P}),e.jsxs("div",{className:"flex-grow flex flex-col items-center justify-center text-center py-10 border-2 border-dashed border-gray-300 dark:border-slate-600 rounded-lg bg-gray-50 dark:bg-slate-800/50",children:[e.jsx(re,{className:"h-12 w-12 text-gray-400 dark:text-slate-500 mb-4"}),e.jsx("h3",{className:"font-semibold text-gray-700 dark:text-slate-200",children:"Generate a Cover Letter"}),e.jsx("p",{className:"text-gray-500 dark:text-slate-400 max-w-sm mx-auto my-2",children:"Create a professional cover letter tailored to the job description and your new resume."}),e.jsx(D,{onClick:k,variant:"primary",size:"lg",disabled:h,className:"flex items-center justify-center space-x-2 bg-blue-600 text-white font-bold py-3 px-6 rounded-lg hover:bg-blue-700 transition-colors mt-4 h-auto border-none",leftIcon:e.jsx(H,{className:"h-5 w-5"}),children:e.jsx("span",{children:p?"Create Free Account":h?"Upgrade to Pro":"Generate Cover Letter"})}),h&&e.jsxs("p",{className:"mt-3 text-xs text-slate-500 dark:text-slate-400",children:["Free credits reset in ",J(C.resetAt),"."]})]})]})},Y="cd_pending_cover_letter_jd",ze=()=>{var l,_;const t=q(),s=ie(),{state:r,actions:c,dispatch:n}=X(),{isGuest:N}=K(),{usage:o}=V(),{coverLetter:u,isGeneratingCoverLetter:i,jobDescription:g,profile:v,session:f}=r,{handleGenerateCoverLetter:M}=c,{wasBackgrounded:F,reset:U}=fe(i),{openSlidePanel:j}=le(),[d,w]=S.useState(g||""),[P,T]=x.useState(!1);x.useEffect(()=>{try{const a=sessionStorage.getItem(Y);a&&!d&&(w(a),n({type:"SET_JOB_DESCRIPTION",payload:a}))}catch{}},[]),x.useEffect(()=>{var m;if(!P||o.tier==="anon")return;T(!1);try{sessionStorage.removeItem(Y)}catch{}const a=G(o.tier,(m=f==null?void 0:f.user)==null?void 0:m.id);o.tier==="signed_in"&&a.remaining<=0||M({jobDescription:d})},[P,o.tier]),x.useEffect(()=>{const a=s.state;a!=null&&a.jobDescription&&(w(a.jobDescription),n({type:"SET_JOB_DESCRIPTION",payload:a.jobDescription}))},[s.state,n]),x.useEffect(()=>{g&&g!==d&&w(g)},[g]);const R=a=>{w(a),n({type:"SET_JOB_DESCRIPTION",payload:a})},C=()=>{var m;if(!d.trim())return;const a=G(o.tier,(m=f==null?void 0:f.user)==null?void 0:m.id);if(o.tier==="anon"){try{sessionStorage.setItem(Y,d)}catch{}T(!0),j({contextMessage:"Create a free account and weâll generate this cover letter right away â your job description is saved.",triggerId:"account_gate_cover_letter"});return}o.tier==="signed_in"&&a.remaining<=0||M({jobDescription:d})},h=v&&(v.fullName||v.experience&&v.experience.length>0),p=S.useMemo(()=>
1de(d),[d]),k=S.useMemo(()=>ce(d),[d]),b=S.useMemo(()=>{const a=p.trim().toLowerCase(),m=k.trim().toLowerCase();return!a&&!m?null:(r.appliedJobs||[]).find(L=>(L.job_title||"").trim().toLowerCase()===a&&(L.company_name||"").trim().toLowerCase()===m)||null},[r.appliedJobs,p,k]),O=S.useMemo(()=>we({role:p,alreadyTracked:!!b,hasLetter:!!u&&!i}),[p,k,b,u,i]),B=S.useCallback(async()=>{try{return b?await ue(b.id,{cover_letter:u||""}):await me({job_title:p||"Untitled role",company_name:k||"Unknown company",job_description:d,tailored_resume:"",cover_letter:u||"",match_score:null,match_justification:"",application_status:"applied",applied_date:new Date().toISOString().split("T")[0]}),!0}catch{return!1}},[b,u,p,k,d]),$=S.useMemo(()=>{var a,m,L;return ye({lastTailoredRole:(a=r.lastTailoredInfo)==null?void 0:a.jobTitle,trackedRole:(L=(m=r.appliedJobs)==null?void 0:m[0])==null?void 0:L.job_title,profile:v})},[(l=r.lastTailoredInfo)==null?void 0:l.jobTitle,r.appliedJobs,v]),y=G(o.tier,(_=f==null?void 0:f.user)==null?void 0:_.id),I=o.tier==="signed_in"&&y.remaining<=0;return e.jsx(ge,{title:"Cover Letter Generator",description:"Generate personalized cover letters for any job application",maxWidth:"4xl",children:e.jsxs("div",{className:"space-y-6",children:[o.tier==="signed_in"&&!I&&e.jsx("div",{className:"flex items-center justify-end gap-2 text-sm text-[var(--text-secondary)]",children:e.jsxs("span",{className:"inline-flex items-center gap-1.5 rounded-full bg-slate-100 dark:bg-slate-800 px-3 py-1 text-xs font-medium",children:[e.jsx("span",{className:`h-2 w-2 rounded-full ${y.remaining>0?"bg-emerald-500":"bg-red-500"}`}),y.remaining," of ",y.limit," cover letter",y.limit!==1?"s":""," remaining today"]})}),N&&!h&&e.jsx(A,{className:"bg-yellow-50 dark:bg-yellow-900/20 border-yellow-200 dark:border-yellow-800",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"font-semibold text-yellow-900 dark:text-yellow-200 mb-1",children:"Profile Required"}),e.jsx("p",{className:"text-sm text-yellow-800 dark:text-yellow-300",children:"You need to create your Career Diary first to generate cover letters."})]}),e.jsx(D,{onClick:()=>t("/app/diary"),variant:"primary",size:"sm",children:"Go to Career Diary"})]})}),(o.tier==="anon"||I)&&e.jsx(A,{className:"border-emerald-200 bg-emerald-50 dark:border-emerald-900/40 dark:bg-emerald-950/20",children:e.jsxs("div",{className:"flex flex-col gap-3 md:flex-row md:items-center md:justify-between",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("h3",{className:"font-semibold text-emerald-900 dark:text-emerald-200",children:o.tier==="anon"?"Create a free account to generate cover letters":"Your free cover letter for today has been used"}),e.jsx("p",{className:"text-sm text-emerald-800 dark:text-emerald-300",children:o.tier==="anon"?"Free accounts get 1 cover letter per day. Pro unlocks unlimited cover letters.":`Free credits reset in ${J(y.resetAt)}. Upgrade to Pro for unlimited cover letters.`})]}),e.jsx(D,{onClick:()=>o.tier==="anon"?j({contextMessage:"Create a free account to generate cover letters â 1 free per day.",triggerId:"account_gate_cover_letter"}):t("/app/settings/billing"),variant:"primary",size:"sm",children:o.tier==="anon"?"Create Free Account":"Upgrade to Pro"})]})}),e.jsx(A,{children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-semibold text-gray-900 dark:text-white mb-2",children:"Job Description"}),e.jsx("textarea",{value:d,onChange:a=>R(a.target.value),placeholder:"Paste the job description here...",className:"w-full h-48 p-4 border rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all bg-white dark:bg-slate-800 text-gray-900 dark:text-white border-gray-300 dark:border-slate-600 placeholder-gray-400 dark:placeholder-slate-400 resize-none",disabled:i})]}),e.jsx(D,{onClick:C,disabled:!d.trim()||i||!h||I,variant:"primary",size:"lg",className:"w-full",children:i?e.jsxs(e.Fragment,{children:[e.jsx(Z,{className:"h-5 w-5"}),"Generating Cover Letter..."]}
1):"Generate Cover Letter"})]})}),F&&e.jsxs("div",{className:"rounded-xl border border-orange-200 bg-orange-50 dark:border-orange-900 dark:bg-orange-950/30 px-4 py-3 flex items-start gap-3",children:[e.jsxs("div",{className:"flex-1 space-y-0.5",children:[e.jsx("p",{className:"text-sm font-semibold text-orange-800 dark:text-orange-200",children:"You left this tab during generation"}),e.jsx("p",{className:"text-xs text-orange-700 dark:text-orange-300 leading-relaxed",children:"Mobile browsers can interrupt long operations. If the cover letter looks incomplete, tap Generate again."})]}),e.jsx("button",{onClick:U,"aria-label":"Dismiss warning",className:"text-orange-600 dark:text-orange-400 hover:text-orange-800 dark:hover:text-orange-200 text-sm",children:"â"})]}),u&&e.jsx(A,{children:e.jsx(De,{})}),u&&!i&&!N&&e.jsx(he,{offer:O,audience:"user",onAccept:B,savedLabel:b?"Filed with your tracked application":"Tracked, with the letter attached"}),u&&!i&&!N&&e.jsx(be,{suggestion:je({context:"cover_letter",role:p}),audience:"user",onAccept:()=>t("/app/applications")}),!u&&!i&&e.jsx(A,{children:e.jsx(ve,{surface:"cover_letter",tease:$})})]})})};export{ze as CoverLetterPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.