import{writeFileSync as P}from"node:fs";import T from"path";import{PAGE_COUNT_OUTPUT_FILE_NAME as y}from"../../constants/common.js";import{readEnvVariable as F}from"../../utils/envs/read-env-variable.js";import{logger as o}from"../../tools/notifiers/logger.js";import{EntitlementsProvider as b}from"../../entitlements/entitlements-provider.js";import{getBilledPagesCount as S,isPathIgnored as O}from"../../utils/index.js";import{isGraphqlDoc as R}from"../graphql-docs/is-graphql-doc.js";import{isMarkdownPage as $}from"../markdown/is-markdown-page.js";import{isOpenapiDoc as E}from"../openapi-docs/is-openapi-doc.js";import{isReactPage as M}from"../pages/is-react-page.js";import{PRODUCT_NAME as l}from"../../../config/product-gates.js";import{isAsyncapiDoc as k}from"../asyncapi-docs/is-asyncapi-doc.js";import{telemetryTraceStep as _}from"../../../cli/telemetry/helpers/trace-step.js";async function Y(){return{id:"entitlements",async afterRoutesCreated(w,u){await _("build.plugin.entitlements",async t=>{const i=b.instance(),r=[],n=[],s=[],a=[],p=[];for(const{relativePath:e}of await u.fs.scan())O(e)||($(e)?r.push(e):R(e)?s.push(e):M(e)?a.push(e):await E(e,u)?n.push(e):await k(e,u)&&p.push(e));const g=i.canAccessFeature("markdown");t?.setAttribute("totalMarkdownFiles",r.length.toString()),t?.setAttribute("isMarkdownSupported",(!!g).toString()),r.length>0&&!g&&o.warn(`The product you are using ("${l}") does not support Markdown pages. The following Markdown files were detected but will be ignored: ${r.map(()=>"%rp").join(", ")}`,...r);const c=i.canAccessFeature("openapi");t?.setAttribute("totalOpenApiFiles",n.length.toString()),t?.setAttribute("isOpenApiSupported",(!!c).toString()),n.length>0&&!c&&o.warn(`The product you are using ("${l}") does not support OpenAPI documents. The following files are ignored: ${n.map(()=>"%rp").join(", ")}`,...n);const d=i.canAccessFeature("asyncapi");t?.setAttribute("totalAsyncApiFiles",p.length.toString()),t?.setAttribute("isAsyncApiSupported",(!!d).toString()),p.length>0&&!d&&o.warn(`The product you are using ("${l}") does not support AsyncAPI documents. The following files are ignored: ${p.map(()=>"%rp").join(", ")}`,...p);const m=i.canAccessFeature("graphql");t?.setAttribute("totalGraphqlFiles",s.length.toString()),t?.setAttribute("isGraphqlSupported",(!!m).toString()),s.length>0&&!m&&o.warn(`The product you are using ("${l}") does not support GraphQL documents. The following files are ignored: ${s.map(()=>"%rp").join(", ")}`,...s);const f=i.canAccessFeature("reactPages");t?.setAttribute("totalReactFiles",a.length.toString()),t?.setAttribute("isReactPagesSupported",(!!f).toString()),a.length>0&&!f&&o.warn(`The product you are using ("${l}") does not support React pages. The following files are ignored: ${a.map(()=>"%rp").join(", ")}`,...a);const A=S(w.getAllRoutes());t?.setAttribute("totalBilledPages",A.toString());const h=F("REDOCLY_METADATA_OUTPUT_FOLDER");h&&(o.info("Save total pages..."),P(T.join(h,y),JSON.stringify({totalPages:A},null)))})}}}export{Y as entitlementsPlugin};
