import o,{useEffect as D,useState as c}from"react";import I from"styled-components";import{useLocation as N}from"react-router-dom";import{Button as C}from"@redocly/theme/components/Button/Button";import{Admonition as k}from"@redocly/theme/markdoc/components/Admonition/Admonition";import{usePageData as T}from"../hooks/usePageData";import{usePreloadHistory as q}from"../usePreloadHistory";import{otelTelemetry as w}from"../telemetry/otel/telemetry";import{useLoginUrl as b}from"../hooks";const O=e=>{const{apiId:s,status:t,setStatus:r}=e,u=q(),{userData:n}=T("userData")||{},h=N(),i=b(),[l,p]=c(),[E,g]=c(),[m,A]=c(!0);D(()=>{n?.isAuthenticated&&fetch(`/api/api-keys/api-products/${s}/access`).then(d=>d.json().then(a=>{d.ok?(r(a.status),g(a.appId)):p(a.message)}).catch(()=>p("Something went wrong")).finally(()=>A(!1)))},[n?.isAuthenticated,s,r]);const P=()=>{!n?.isAuthenticated&&i?window.location.href=`${i}${i.includes("?")?"&":"?"}redirectTo=${h.pathname}`:t==="ACCEPTED"?u.push("/apps/"+E||""):t==="NONE"&&u.push("/apps/?createApp&apiId="+s),w.send({type:"request_api_access_button.clicked",payload:{action:"click"}})},y=(t==="PENDING"||m||l!=null)&&n?.isAuthenticated;return o.createElement(o.Fragment,null,o.createElement(C,{disabled:y,onClick:P,fullWidth:!0,size:"small"},x(t,m,l,!!n?.isAuthenticated)))},x=(e,s,t,r)=>{if(!r)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},U=e=>e==="ACCEPTED"?o.createElement(f,{type:"success",name:"You have access to this API"}):e==="PENDING"?o.createElement(f,{type:"info",name:"You have requested access to this API"}):null,f=I(k)`
  margin-top: 0px;
  margin-bottom: var(--spacing-sm);
`;export{O as RequestAccessButton,U as renderAdmonition};
