import{ENTITLEMENTS_JWKS_URL as o}from"../constants/common.js";import{PLAN_GATES_DEFAULTS as c}from"../constants/entitlements.js";import{envConfig as s}from"../config/env-config.js";import{logger as a}from"../tools/notifiers/logger.js";import{reporter as l}from"../tools/notifiers/reporter.js";import{PRODUCT_GATES as m}from"../../config/product-gates.js";import{EJECT_IGNORE_CONFIG as p}from"../../config/eject-ignore.js";import*as i from"../web-server/jwt/jwt.js";import{AlgorithmTypes as f}from"../web-server/jwt/types.js";class r{static#e;#a=null;#t={};constructor(){}static instance(){return r.#e||(r.#e=new r),r.#e}get entitlements(){return this.#a}async init(t={}){this.#t=t;try{await this.#i()}catch(e){a.warn("Failed to process entitlements information: "+e.message),a.verbose(e.message)}}canAccessFeature=t=>t?this.entitlements&&!!this.entitlements[t]:!0;async getPackageEjectIgnore(){let t=[];for(const[e,n]of Object.entries(p))this.canAccessFeature(e)&&(t=[...t,...n]);return t}#n(){return s.PLAN_GATES||""}async#r(){let t;try{t=await fetch(o).then(e=>e.json())}catch(e){return a.warn("Failed to get JWKS information: "+e.message),!1}try{const e=t.keys&&t.keys.length>0?t.keys[0]:null,n=this.#n();return e&&n?await i.verify(n,e,f.RS256,this.#t.ignoreTokenExpiration):!1}catch(e){return a.warn("Failed to verify plan gates token: "+e.message),!1}}async#s(){if(await this.#r()){const{payload:t}=i.decode(this.#n());if(t){if(!s.REDOCLY_ENV&&t.exp){const e=t.exp*1e3,n=new Date(e);a.info(`Entitlements token expires on ${n.toISOString()} (in ${Math.ceil((e-new Date().getTime())/(1440*60*1e3))} days)`)}return t}}await l.panicOnContentError("Plan gates token is not valid or empty.")}async#i(){const t=this.#t.developModePlan&&!this.#n()?c[this.#t.developModePlan]:await this.#s();this.#a={...m,...t}}}export{r as EntitlementsProvider};
