(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["object"==typeof document?document.currentScript:void 0,18404,e=>{"use strict";e.s(["AuthProvider",()=>l,"useAuthContext",()=>c],18404);var t=e.i(30064),r=e.i(59760),s=e.i(35828),a=e.i(4301);function n({children:e}){let{user:s}=c();return(0,r.useEffect)(()=>{if(!s)return;let e=s.preferences?.theme||"light",t=document.documentElement;t.classList.remove("theme-light","theme-dark","theme-sepia"),t.classList.add(`theme-${e}`)},[s?.preferences?.theme]),(0,t.jsx)(t.Fragment,{children:e})}let i=(0,r.createContext)(null);function o({children:e}){let o=(()=>{let{data:e,status:t}=(0,s.useSession)(),[n,i]=(0,r.useState)(null),o="loading"===t;(0,r.useEffect)(()=>{e?.user?.id?fetch("/api/user/profile",{cache:"no-store"}).then(e=>e.json()).then(e=>{let t=e.plan||"free",r=e.planDetails||{id:"free",displayName:"Gratuit",maxAiActions:100,maxProjects:3};i({id:e.id,email:e.email,name:e.name||"User",avatar:e.avatar,bio:e.bio,subscription:{plan:t,planDetails:r,startDate:new Date(e.createdAt).getTime(),status:"active"},usage:{aiActionsCurrent:e.aiActionsUsed||0,aiActionsLimit:r.maxAiActions,projectsLimit:r.maxProjects},preferences:{theme:"light",dailyWordGoal:e.dailyWordGoal||500,language:"fr"},stats:{totalWordsWritten:e.totalWords||0,writingStreak:e.writingStreak||0,lastWriteDate:e.lastWriteDate?new Date(e.lastWriteDate).getTime():0}})}).catch(t=>{console.error("Failed to fetch user profile:",t),i({id:e.user.id||"",email:e.user.email||"",name:e.user.name||"User",subscription:{plan:"free",startDate:Date.now(),status:"active"},usage:{aiActionsCurrent:0,aiActionsLimit:100,projectsLimit:3},preferences:{theme:"light",dailyWordGoal:500,language:"fr"},stats:{totalWordsWritten:0,writingStreak:0,lastWriteDate:0}})}):"unauthenticated"===t&&i(null)},[e,t]);let l=(0,r.useCallback)(async(e,t)=>{let r=await (0,s.signIn)("credentials",{email:e,password:t,redirect:!1});if(r?.error)throw Error("CredentialsSignin"===r.error?"Email ou mot de passe incorrect":r.error)},[]),c=(0,r.useCallback)(async(e,t,r)=>{await a.default.auth.register(e,t,r);let n=await (0,s.signIn)("credentials",{email:e,password:t,redirect:!1});if(n?.error)throw Error("Compte créé mais erreur de connexion automatique")},[]),u=(0,r.useCallback)(async()=>{await (0,s.signOut)({redirect:!1}),i(null)},[]),d=(0,r.useCallback)(e=>{n&&i({...n,usage:{...n.usage,aiActionsCurrent:void 0!==e?e:n.usage.aiActionsCurrent+1}})},[n]),h=(0,r.useCallback)(async e=>{if(n)try{let t={};void 0!==e.name&&(t.name=e.name),void 0!==e.avatar&&(t.avatar=e.avatar),void 0!==e.bio&&(t.bio=e.bio),e.preferences?.dailyWordGoal!==void 0&&(t.dailyWordGoal=e.preferences.dailyWordGoal),await a.default.user.updateProfile(t),i(t=>t?{...t,...e}:null)}catch(e){throw console.error("Failed to update profile:",e),e}},[n]);return{user:n,login:l,signup:c,logout:u,incrementUsage:d,updateProfile:h,loading:o}})();return(0,t.jsx)(i.Provider,{value:o,children:(0,t.jsx)(n,{children:e})})}let l=({children:e})=>(0,t.jsx)(s.SessionProvider,{children:(0,t.jsx)(o,{children:e})});function c(){return(0,r.useContext)(i)}},4301,e=>{"use strict";let t={async request(e,t={}){let r=`/api${e}`,s={"Content-Type":"application/json",...t.headers},a=await fetch(r,{...t,headers:s});if(!a.ok){let e=`Error ${a.status}: ${a.statusText}`;try{let t=await a.json();t.error&&(e=t.error),t.message&&(e=t.message)}catch{}throw Error(e)}return 204===a.status?null:a.json()},auth:{register:async(e,r,s)=>t.request("/auth/register",{method:"POST",body:JSON.stringify({email:e,password:r,name:s})})},user:{updateProfile:async e=>t.request("/user/profile",{method:"PUT",body:JSON.stringify(e)})},projects:{list:async()=>t.request("/projects"),get:async e=>t.request(`/projects/${e}`),create:async e=>t.request("/projects",{method:"POST",body:JSON.stringify(e)}),update:async(e,r)=>t.request(`/projects/${e}`,{method:"PUT",body:JSON.stringify(r)}),delete:async e=>t.request(`/projects/${e}`,{method:"DELETE"}),syncWorkflow:async(e,r)=>t.request(`/projects/${e}/workflow`,{method:"PUT",body:JSON.stringify(r)})},chapters:{create:async e=>t.request("/chapters",{method:"POST",body:JSON.stringify(e)}),update:async(e,r)=>t.request(`/chapters/${e}`,{method:"PUT",body:JSON.stringify(r)}),delete:async e=>t.request(`/chapters/${e}`,{method:"DELETE"})},entities:{create:async e=>t.request("/entities",{method:"POST",body:JSON.stringify(e)}),update:async(e,r)=>t.request(`/entities/${e}`,{method:"PUT",body:JSON.stringify(r)}),delete:async e=>t.request(`/entities/${e}`,{method:"DELETE"})},ideas:{create:async e=>t.request("/ideas",{method:"POST",body:JSON.stringify(e)}),update:async(e,r)=>t.request(`/ideas/${e}`,{method:"PUT",body:JSON.stringify(r)}),delete:async e=>t.request(`/ideas/${e}`,{method:"DELETE"})},ai:{generate:async(e,r,s,a)=>t.request("/ai/generate",{method:"POST",body:JSON.stringify({project:e,chapterId:r,prompt:s,user:a})}),transform:async(e,r,s,a)=>(await t.request("/ai/transform",{method:"POST",body:JSON.stringify({text:e,mode:r,context:s,user:a})})).text}};e.s(["default",0,t])},35828,e=>{"use strict";var t=e.i(58865),r=e.i(30064),s=e.i(59760);class a extends Error{constructor(e,t){e instanceof Error?super(void 0,{cause:{err:e,...e.cause,...t}}):"string"==typeof e?(t instanceof Error&&(t={err:t,...t.cause}),super(e,t)):super(void 0,e),this.name=this.constructor.name,this.type=this.constructor.type??"AuthError",this.kind=this.constructor.kind??"error",Error.captureStackTrace?.(this,this.constructor);const r=`https://errors.authjs.dev#${this.type.toLowerCase()}`;this.message+=`${this.message?". ":""}Read more at ${r}`}}class n extends a{}class i extends a{}async function o(e,t,r,s={}){let a=`${l(t)}/${e}`;try{let e={headers:{"Content-Type":"application/json",...s?.headers?.cookie?{cookie:s.headers.cookie}:{}}};s?.body&&(e.body=JSON.stringify(s.body),e.method="POST");let t=await fetch(a,e),r=await t.json();if(!t.ok)throw r;return r}catch(e){return r.error(new n(e.message,e)),null}}function l(e){return"u"a}}let d={baseUrl:u(t.default.env.NEXTAUTH_URL??t.default.env.VERCEL_URL).origin,basePath:u(t.default.env.NEXTAUTH_URL).path,baseUrlServer:u(t.default.env.NEXTAUTH_URL_INTERNAL??t.default.env.NEXTAUTH_URL??t.default.env.VERCEL_URL).origin,basePathServer:u(t.default.env.NEXTAUTH_URL_INTERNAL??t.default.env.NEXTAUTH_URL).path,_lastSync:0,_session:void 0,_getSession:()=>{}},h=null;function f(){return"u"{},addEventListener:()=>{},removeEventListener:()=>{},name:"next-auth",onmessage:null,onmessageerror:null,close:()=>{},dispatchEvent:()=>!1}:new BroadcastChannel("next-auth")}function g(){return null===h&&(h=f()),h}let m={debug:console.debug,error:console.error,warn:console.warn},y=s.createContext?.(void 0);function p(e){if(!y)throw Error("React Context is unavailable in Server Components");let t=s.useContext(y),{required:r,onUnauthenticated:a}=e??{},n=r&&"unauthenticated"===t.status;return(s.useEffect(()=>{if(n){let e=`${d.basePath}/signin?${new URLSearchParams({error:"SessionRequired",callbackUrl:window.location.href})}`;a?a():window.location.href=e}},[n,a]),n)?{data:t.data,update:t.update,status:"loading"}:t}async function w(e){let t=await o("session",d,m,e);return(e?.broadcast??!0)&&f().postMessage({event:"session",data:{trigger:"getSession"}}),t}async function v(){let e=await o("csrf",d,m);return e?.csrfToken??""}async function S(){return o("providers",d,m)}async function b(e,t,r){let{callbackUrl:s,...a}=t??{},{redirect:n=!0,redirectTo:i=s??window.location.href,...o}=a,c=l(d),u=await S();if(!u){let e=`${c}/error`;window.location.href=e;return}if(!e||!u[e]){let e=`${c}/signin?${new URLSearchParams({callbackUrl:i})}`;window.location.href=e;return}let h=u[e].type;if("webauthn"===h)throw TypeError(`Provider id "${e}" refers to a WebAuthn provider. Please use \`import { signIn } from "next-auth/webauthn"\` instead.`);let f=`${c}/${"credentials"===h?"callback":"signin"}/${e}`,g=await v(),m=await fetch(`${f}?${new URLSearchParams(r)}`,{method:"post",headers:{"Content-Type":"application/x-www-form-urlencoded","X-Auth-Return-Redirect":"1"},body:new URLSearchParams({...o,csrfToken:g,callbackUrl:i})}),y=await m.json();if(n){let e=y.url??i;window.location.href=e,e.includes("#")&&window.location.reload();return}let p=new URL(y.url).searchParams.get("error")??void 0,w=new URL(y.url).searchParams.get("code")??void 0;return m.ok&&await d._getSession({event:"storage"}),{error:p,code:w,status:m.status,ok:m.ok,url:p?null:y.url}}async function E(e){let{redirect:t=!0,redirectTo:r=e?.callbackUrl??window.location.href}=e??{},s=l(d),a=await v(),n=await fetch(`${s}/signout`,{method:"post",headers:{"Content-Type":"application/x-www-form-urlencoded","X-Auth-Return-Redirect":"1"},body:new URLSearchParams({csrfToken:a,callbackUrl:r})}),i=await n.json();if(g().postMessage({event:"session",data:{trigger:"signout"}}),t){let e=i.url??r;window.location.href=e,e.includes("#")&&window.location.reload();return}return await d._getSession({event:"storage"}),i}function T(e){if(!y)throw Error("React Context is unavailable in Server Components");let{children:t,basePath:a,refetchInterval:n,refetchWhenOffline:l}=e;a&&(d.basePath=a);let u=void 0!==e.session;d._lastSync=u?c():0;let[h,f]=s.useState(()=>(u&&(d._session=e.session),e.session)),[p,S]=s.useState(!u);s.useEffect(()=>(d._getSession=async({event:e}={})=>{try{let t="storage"===e;if(t||void 0===d._session){d._lastSync=c(),d._session=await w({broadcast:!t}),f(d._session);return}if(!e||null===d._session||c(){d._lastSync=0,d._session=void 0,d._getSession=()=>{}}),[]),s.useEffect(()=>{let e=()=>d._getSession({event:"storage"});return g().addEventListener("message",e),()=>g().removeEventListener("message",e)},[]),s.useEffect(()=>{let{refetchOnWindowFocus:t=!0}=e,r=()=>{t&&"visible"===document.visibilityState&&d._getSession({event:"visibilitychange"})};return document.addEventListener("visibilitychange",r,!1),()=>document.removeEventListener("visibilitychange",r,!1)},[e.refetchOnWindowFocus]);let b=function(){let[e,t]=s.useState("u">typeof navigator&&navigator.onLine),r=()=>t(!0),a=()=>t(!1);return s.useEffect(()=>(window.addEventListener("online",r),window.addEventListener("offline",a),()=>{window.removeEventListener("online",r),window.removeEventListener("offline",a)}),[]),e}(),E=!1!==l||b;s.useEffect(()=>{if(n&&E){let e=setInterval(()=>{d._session&&d._getSession({event:"poll"})},1e3*n);return()=>clearInterval(e)}},[n,E]);let T=s.useMemo(()=>({data:h,status:p?"loading":h?"authenticated":"unauthenticated",async update(e){if(p)return;S(!0);let t=await o("session",d,m,void 0===e?void 0:{body:{csrfToken:await v(),data:e}});return S(!1),t&&(f(t),g().postMessage({event:"session",data:{trigger:"getSession"}})),t}}),[h,p]);return(0,r.jsx)(y.Provider,{value:T,children:t})}e.s(["SessionProvider",()=>T,"signIn",()=>b,"signOut",()=>E,"useSession",()=>p],35828)}]);