import{ENTITLEMENTS_JWKS_URL as c}from"../constants/common.js";import{PLAN_GATES_DEFAULTS as l}from"../constants/entitlements.js";import{readEnvVariable as i}from"../utils/envs/read-env-variable.js";import{logger as s}from"../tools/notifiers/logger.js";import*as r from"../web-server/jwt/jwt.js";import{AlgorithmTypes as m}from"../web-server/jwt/types.js";import{PRODUCT_GATES as p}from"../../config/product-gates.js";import{EJECT_IGNORE_CONFIG as f}from"../../config/eject-ignore.js";class a{static#e;#s=null;#t={};constructor(){}static instance(){return a.#e||(a.#e=new a),a.#e}get entitlements(){return this.#s}async init(t={}){this.#t=t;try{await this.#r()}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(f))this.canAccessFeature(e)&&(t=[...t,...n]);return t}#n(){return i("PLAN_GATES")||""}async#a(){try{const t=await fetch(c).then(o=>o.json()),e=t.keys&&t.keys.length>0?t.keys[0]:null,n=this.#n();return e&&n?await r.verify(n,e,m.RS256,this.#t.ignoreTokenExpiration):!1}catch(t){return s.warn("Failed to get JWKS information: "+t.message),!1}}async#i(){if(await this.#a()){const{payload:t}=r.decode(this.#n());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())/(1440*60*1e3))} days)`)}return t}}s.panicOnContentError("Plan gates token is not valid or empty.")}async#r(){const t=this.#t.developModePlan&&!this.#n()?l[this.#t.developModePlan]:await this.#i();this.#s={...p,...t}}}export{a as EntitlementsProvider};
