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