import n,{useEffect as D,useState as a}from"react";import C from"styled-components";import{Button as I}from"@redocly/theme/components/Button/Button";import{Admonition as N}from"@redocly/theme/markdoc/components/Admonition/Admonition";import{withPathPrefix as y,getBaseDataAttributes as k}from"@redocly/theme/core/utils";import{usePageData as q}from"../hooks/usePageData";import{usePreloadHistory as w}from"../usePreloadHistory";import{telemetry as b}from"../telemetry/index.js";import{useLoginUrl as x}from"../hooks";const U=e=>{const{apiId:s,status:t,setStatus:i}=e,c=w(),{userData:r}=q("userData")||{},u=x(),[l,p]=a(),[h,A]=a(),[m,g]=a(!0);D(()=>{r?.isAuthenticated&&fetch(`/api/api-keys/api-products/${s}/access`).then(d=>d.json().then(o=>{d.ok?(i(o.status),A(o.appId)):p(o.message)}).catch(()=>p("Something went wrong")).finally(()=>g(!1)))},[r?.isAuthenticated,s,i]);const E=()=>{!r?.isAuthenticated&&u?window.location.href=y(u):t==="ACCEPTED"?c.push("/apps/"+h||""):t==="NONE"&&c.push("/apps/?createApp&apiId="+s),b.sendRequestApiAccessClickedMessage([k("requestApiAccess","button")])},P=(t==="PENDING"||m||l!=null)&&r?.isAuthenticated;return n.createElement(n.Fragment,null,n.createElement(I,{disabled:P,onClick:E,fullWidth:!0,size:"small"},R(t,m,l,!!r?.isAuthenticated)))},R=(e,s,t,i)=>{if(!i)return"Request access";if(s)return"Loading...";if(t)return"Failed to check access";switch(e){case"NONE":return"Request access";case"ACCEPTED":return"Credentials";case"PENDING":return"Pending access..."}return null},Y=e=>e==="ACCEPTED"?n.createElement(f,{type:"success",name:"You have access to this API"}):e==="PENDING"?n.createElement(f,{type:"info",name:"You have requested access to this API"}):null,f=C(N)`
  margin-top: 0px;
  margin-bottom: var(--spacing-sm);
`;export{U as RequestAccessButton,Y as renderAdmonition};
