import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{Cu as t,Eu as r,L_ as n,Tu as o,hi as s,sl as i}from"./shared-0-C4kdVF7Y.js";import{Mi as a,ki as u}from"./vendor-frame-CwiylG4Y.js";import{Aa as c,Ea as l,Na as d,Oa as m,Ra as f,Ta as g,wa as p,xa as h,ya as k}from"./shared-1-3-6x7RKF.js";import{Ia as y,Ra as _,_o as w}from"./shared-5-UQAFGbWu.js";import{Nc as S}from"./shared-2-CMUBhYOn.js";import{cn as v,ln as b}from"./shared-14-BNdP3DJt.js";var $=e(a()),z=new Set(["server","step","flow_id","ema_error_code"]),j=/^(oauth_error|entra_)/,M=new Set(["auth_start","auth_start_org","ghe_config_id","origin","open_in_browser","open_outline_auth","redirect_url"]),N=new Set(["modal","connectorName","connectorUrl"]);function R(e,t){return z.has(e)||j.test(e)||M.has(e)||"add-custom-connector"===t.get("modal")&&N.has(e)}var E=new Set(["from","search"]),Q=/^(utm_|gb_(gate|feature)_)/;function x(){const e=o(),a=t(),u=r(),z=f(e=>e.loadFailed),j=v(),M=b(),{groups:N,isPending:x}=w({forModal:!0}),I=(0,$.useMemo)(()=>new Set(N.flatMap(e=>e.sections).map(e=>e.id)),[N]),T=y(),A=n("mcp_connector_directory_cxdir_v2"),C=null!==u.get("browse"),F="true"===u.get("directory"),L=/^\/customize\/plugins\/new\/?$/.test(a)&&u.get("marketplace")||null,P=u.get("plugin"),{isEnabled:U}=S(),O=i(),q=(0,$.useMemo)(()=>Array.from(u.keys()).some(e=>R(e,u)),[u]),[B,D]=(0,$.useState)(!1);(0,$.useEffect)(()=>{if(!q||!M||B)return;const e=setTimeout(()=>D(!0),5e3);return()=>clearTimeout(e)},[q,M,B]);const G=null!==L&&O,H=(0,$.useMemo)(()=>{if(z)return{kind:"reload"};const e=d(a,u.toString()),t=null!==e&&c(e.section)?e.section:null,r=new Set([...null!==t?k[t]??[]:[],"browse","directory",...null!==L?["marketplace","plugin"]:[]]),n=new URLSearchParams;let o=!1;for(const a of u.keys())R(a,u)?n.set(a,u.get(a)??""):r.has(a)||E.has(a)||Q.test(a)||(o=!0);if(o&&"customize-connectors"===t)return{kind:"route",href:`/settings/connectors?${u.toString()}`};const s=e=>{const t=n.toString();return t?`${e}${e.includes("?")?"&":"?"}${t}`:e};if(q&&M&&!B)return{kind:"pending"};if(null===j){const e=Array.from(n.keys()).length>0;return"customize-connectors"===t&&e?{kind:"route",href:s("/settings/connectors")}:{kind:"route",href:s("/settings")}}if(G)return{kind:"pending"};if(null!==L&&U)return{kind:"directory",host:s(j),section:"plugins",install:{marketplaceSource:L,pluginName:P??void 0}};if(C)return{kind:"directory",host:s(j),section:"plugins"};if(F){if(x||O)return{kind:"pending"};if(I.has("customize-connectors"))return A?{kind:"modal",host:j,section:"customize-connectors",subpath:_,carryQuery:n.toString()}:{kind:"directory",host:s(j),section:"connectors"}}if(null===t)return{kind:"route",href:s(j)};let i=null!==e?e.subpath:void 0;if("customize-plugins"===t&&"new"===l(i)[0]&&(i=void 0),"customize-plugins"===t&&!p(t,i)){const e=l(i);e.length>1&&null===g(e[1])?e.length=1:e.length>2&&(e.length=2),i=m(e)}if(!I.has(t)){if(x||O)return{kind:"pending"};if("customize-memory"===t)return{kind:"route",href:s("/settings/capabilities")};if(/^\/customize\/?$/.test(a)){const e=N.flatMap(e=>e.sections).map(e=>e.id).find(e=>c(e));if(void 0!==e)return{kind:"modal",host:j,section:e,subpath:void 0,carryQuery:n.toString()}}return{kind:"route",href:s(j)}}return{kind:"modal",host:j,section:t,subpath:i,carryQuery:n.toString()}},[z,j,q,M,B,G,a,u,I,N,x,O,L,P,U,C,F,A]),J=(0,$.useRef)(!1);return(0,$.useEffect)(()=>{if("pending"===H.kind||J.current)return;if(J.current=!0,"reload"===H.kind){let t;try{t=null===window.sessionStorage.getItem("customizeChunkFailReloaded")?"armed":"spent"}catch{t="unavailable"}if("armed"===t)try{return window.sessionStorage.setItem(h,"1"),void window.location.reload()}catch{}return void e.replace("/settings")}if("route"===H.kind)return void e.replace(H.href);if("directory"===H.kind)return T({section:H.section},void 0!==H.install?{pluginsInitialConfig:{marketplaceSource:H.install.marketplaceSource,pluginName:H.install.pluginName}}:void 0),void e.replace(H.host);s.getState().close(),f.getState().openFromRoute(H.section,H.subpath);const t=H.subpath?`#settings/${H.section}/${H.subpath}`:`#settings/${H.section}`,r=H.carryQuery?`?${H.carryQuery}`:"";e.replace(`${H.host}${r}${t}`)},[H,e,T]),null}var I=u();function T(){return(0,I.jsx)(x,{})}export{T as CustomizeRedirectRoute};