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