"use strict";var __defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var r in t)__defProp(e,r,{get:t[r],enumerable:!0})},__copyProps=(t,r,o,a)=>{if(r&&"object"==typeof r||"function"==typeof r)for(let e of __getOwnPropNames(r))__hasOwnProp.call(t,e)||e===o||__defProp(t,e,{get:()=>r[e],enumerable:!(a=__getOwnPropDesc(r,e))||a.enumerable});return t},__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),server_init_exports={},tw=(__export(server_init_exports,{after:()=>after,name:()=>name,platforms:()=>platforms,startup:()=>startup,synchronous:()=>synchronous}),module.exports=__toCommonJS(server_init_exports),$tw),SERVER_PROBE_REFRESH_TITLE="$:/temp/CPL-Server/probe-refresh-token",OAUTH_STATE_KEY="cpl-oauth-state",OAUTH_RETURN_KEY="cpl-oauth-return",getCurrentServerOrigin=()=>{var e=tw.wiki.getTiddlerText("$:/plugins/Gk0Wk/CPL-Repo/config/current-server-repo",tw.wiki.getTiddlerText("$:/plugins/Gk0Wk/CPL-Repo/config/current-server","")).trim();for(const o of[tw.wiki.getTiddlerText("$:/temp/CPL-Server/api-base","").trim(),e])if(o)try{var t=new URL(o,window.location.origin),r=t.pathname.replace(/\/$/,"");return r.endsWith("/repo")&&(t.pathname=r.slice(0,-"/repo".length)||"/"),t.toString().replace(/\/$/,"")}catch{}return""},generateOAuthState=()=>{var t=new Uint8Array(16);if("undefined"!=typeof crypto&&crypto.getRandomValues)crypto.getRandomValues(t);else for(let e=0;e<t.length;e++)t[e]=Math.floor(256*Math.random());return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join("")},handleGithubLogin=()=>{var e=tw.wiki.getTiddlerText("$:/temp/CPL-Server/github-client-id","").trim();if(e&&"null"!==e.toLowerCase()&&"undefined"!==e.toLowerCase()){var t=generateOAuthState();try{sessionStorage.setItem(OAUTH_STATE_KEY,t),sessionStorage.setItem(OAUTH_RETURN_KEY,window.location.href)}catch{}var r=getCurrentServerOrigin()+"/cpl/auth/github/callback",e="https://github.com/login/oauth/authorize?"+new URLSearchParams({client_id:e,redirect_uri:r,scope:"read:user",state:t}).toString();window.location.href=e}else console.error("[CPL-Server] GitHub client ID not available. Server may not have OAuth configured.")},handleOAuthCallback=()=>{var t=new URLSearchParams(window.location.search),r=t.get("cpl_oauth_code"),o="/cpl/auth/github/callback"===window.location.pathname;if(r||o){o=r||t.get("code"),r=t.get("cpl_oauth_state")||t.get("state");let a="/",e=!1;try{var i=sessionStorage.getItem(OAUTH_STATE_KEY),n=sessionStorage.getItem(OAUTH_RETURN_KEY);i&&r===i&&(e=!0,n)&&(a=n),sessionStorage.removeItem(OAUTH_STATE_KEY),sessionStorage.removeItem(OAUTH_RETURN_KEY)}catch{}e?o?(window.history?.replaceState&&window.history.replaceState({},document.title,""+window.location.pathname+(window.location.hash||"")),tw.utils.httpRequest({url:getCurrentServerOrigin()+"/cpl/auth/github/callback?code="+encodeURIComponent(o),type:"GET",headers:{"Content-Type":"application/json"},callback:(e,t)=>{if(!e&&t)try{var r=JSON.parse(t);r.success&&(tw.wiki.addTiddler({title:"$:/temp/CPL-Server/user-status",text:"authenticated"}),tw.wiki.addTiddler({title:"$:/temp/CPL-Server/user",text:JSON.stringify(r.user),type:"application/json"}),tw.wiki.addTiddler({title:"$:/temp/CPL-Server/auth-refresh-token",text:String(Date.now())}),window.location.replace(a))}catch(o){console.error("[CPL-Server] Failed to parse auth response:",o)}}})):window.location.replace("/"):(console.error("[CPL-Server] OAuth state mismatch. Possible CSRF attack."),tw.wiki.addTiddler({title:"$:/temp/CPL-Server/oauth-error",text:"OAuth state mismatch. Please try logging in again."}),window.location.replace("/"))}},name="cpl-server-api-client",platforms=["browser"],after=["startup"],synchronous=!0,CPL_LAYOUT_TITLE="$:/plugins/Gk0Wk/CPL-Repo/layout/layout",AUTO_LOAD_TRIGGER_TITLE="$:/temp/CPL-Repo/auto-load-trigger",PLUGINS_INDEX_TITLE="$:/temp/CPL-Repo/plugins-index",setupGithubLoginRequest=()=>{tw.rootWidget.addEventListener("cpl-github-login",()=>{handleGithubLogin()})},startup=()=>{tw.cpl={},tw.wiki.addTiddler({title:SERVER_PROBE_REFRESH_TITLE,text:String(Date.now())});var e=tw.wiki.getTiddlerText("$:/plugins/Gk0Wk/CPL-Repo/config/auto-load-database-in-cpl-layout","yes"),t=tw.wiki.getTiddlerText("$:/layout","");"yes"!==e||t!==CPL_LAYOUT_TITLE||tw.wiki.getTiddler(PLUGINS_INDEX_TITLE)||tw.wiki.addTiddler({title:AUTO_LOAD_TRIGGER_TITLE,text:String(Date.now())}),setupGithubLoginRequest(),handleOAuthCallback(),console.log("[CPL-Server] API Client initialized")};