import{ok as s}from"../modules/ok.js";import{hasPermission as e,setImpliedBy as t}from"../util/permissions.js";export default s.proxy({config:null,session:null,catalog:null,loading:!1,error:null,notices:[],noticesDismissed:!1,get mode(){return this.config?.mode??"open"},get openAccess(){return!!this.config?.openAccess},get isAuthenticated(){return!!this.session?.authenticated},get user(){return this.session?.user??null},get grants(){return this.session?.grants??{}},get username(){return this.user?.username??(this.openAccess?"system":null)},get rolesLabel(){const s=this.user?.roles;return Array.isArray(s)&&s.length?s.join(", "):""},can(s){if(this.openAccess)return!0;const t=this.user?.roles;return!!t&&e(t,s)},canForEnv(s,t){if(this.openAccess)return!0;if(this.can(t))return!0;const n=this.grants[s];return!(!Array.isArray(n)||0===n.length)&&e(n,t)},_grantsMatchAny(s){for(const t of Object.values(this.grants))if(Array.isArray(t))for(const n of s)if(e(t,n))return!0;return!1},canAny(s){return Array.isArray(s)&&s.some(s=>this.can(s))},canSection(s){if(this.openAccess)return!0;switch(s){case"db":return this.canAny(["data:read","schema:read","search:query"])||this._grantsMatchAny(["data:read","schema:read","search:query","data:*","schema:*","search:*","*"]);case"engines":case"system":return this.can("system:read");case"files":return this.can("files:read")||this._grantsMatchAny(["files:read","files:*","*"]);case"queue":return this.canAny(["queue:read","queue:work"])||this._grantsMatchAny(["queue:read","queue:work","queue:*","*"]);case"functions":return this.canAny(["functions:read","functions:run"])||this._grantsMatchAny(["functions:read","functions:run","functions:*","*"]);case"auth":return this.can("auth:read");case"sync":return this.can("sync:read");default:return!1}},permTooltip(s){return this.can(s)?"":`Requires ${s} permission`},get modeLabel(){return this.openAccess?"Open access":"secure"===this.mode?"Secure":String(this.mode||"auth")},get visibleNotices(){return this.noticesDismissed?[]:this.notices||[]},dismissNotices(){this.noticesDismissed=!0;try{sessionStorage.setItem("okdb_notices_dismissed","1")}catch{}},async loadConfig(){const s=await fetch("./login/config",{credentials:"same-origin"}),e=await s.json().catch(()=>({}));return this.config=e?.data||{},this.config},async loadSession(){if(this.openAccess)return this.session={authenticated:!0,user:{username:"system",roles:["admin"]},grants:{}},this.session;const s=await fetch("./session",{credentials:"same-origin"});if(!s.ok)return this.session={authenticated:!1,user:null,grants:{}},this.session;const e=await s.json().catch(()=>({})),t=e?.data||e?.result||{authenticated:!1,user:null};return this.session={grants:{},...t},this.session},async loadCatalog(){const s=await fetch("./api/auth/permissions",{credentials:"same-origin"});if(!s.ok)return null;const e=await s.json().catch(()=>({})),n=e?.data||e?.result||null;return n?.implies&&t(n.implies),this.catalog=n,n},async loadNotices(){try{if("1"===sessionStorage.getItem("okdb_notices_dismissed"))return void(this.noticesDismissed=!0);const s=await fetch("./api/notices",{credentials:"same-origin"});if(s.ok){const e=await s.json().catch(()=>({}));this.notices=e?.data?.notices??e?.result?.notices??[]}}catch{}},async init(){this.loading=!0,this.error=null;try{await this.loadConfig(),await this.loadSession(),await this.loadCatalog(),await this.loadNotices()}catch(s){throw this.error=s?.message||String(s),s}finally{this.loading=!1}},async logout(){await fetch("/auth/logout",{method:"POST",credentials:"same-origin",headers:{"content-type":"application/json"}}).catch(()=>{}),this.session={authenticated:!1,user:null};const s=encodeURIComponent("/admin/index.html");window.location.replace(`/admin/login?redirect=${s}`)}});