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