import{r as e}from"./rolldown-runtime-hePW80VL.js";import{n as t}from"./jsx-runtime-DE3RlOCf.js";import{t as n}from"./client-Avph22il.js";var r=e(t(),1),i=new Map,a=new Map,o=new Set;function s(){o.forEach(e=>e())}function c(e){let t=new Map;return(Array.isArray(e)?e:Object.values(e.groups??{}).flat()).forEach(e=>{e.value_type===`boolean`?t.set(e.code,e.value?.enabled||!1):e.value_type===`choice`&&t.set(e.code,!!e.value?.selected)}),t}async function l(e){if(!e||i.has(e))return;if(a.has(e))return a.get(e);let t=(async()=>{try{let t=await n.get(`/api/v1/menu/module-config/${e}/flags/`);i.set(e,c(t)),s()}catch{i.set(e,new Map),s()}finally{a.delete(e)}})();return a.set(e,t),t}function u(){let[e,t]=(0,r.useState)(i);return(0,r.useEffect)(()=>{let e=()=>t(new Map(i));return o.add(e),()=>{o.delete(e)}},[]),e}function d(e){let[t,a]=(0,r.useState)(!1),[o,s]=(0,r.useState)(!0);return(0,r.useEffect)(()=>{if(!e){a(!1),s(!1);return}let[t,r]=e.split(`.`);if(!t||!r){console.warn(`Invalid feature flag path: ${e}. Expected format: 'module_code.flag_code'`),a(!1),s(!1);return}let o=i.get(t);if(o&&o.has(r)){a(o.get(r)||!1),s(!1);return}(async()=>{try{s(!0);let e=await n.get(`/api/v1/menu/module-config/${t}/flags/`),o=new Map,c=[];Array.isArray(e)?c=e:e&&`groups`in e&&typeof e.groups==`object`&&(c=Object.values(e.groups).flat()),c.forEach(e=>{e.value_type===`boolean`?o.set(e.code,e.value?.enabled||!1):e.value_type===`choice`&&o.set(e.code,!!e.value?.selected)}),i.set(t,o),a(o.get(r)||!1)}catch(e){console.error(`Failed to fetch feature flags for ${t}:`,e),a(!1)}finally{s(!1)}})()},[e]),t}function f(e){let[t,n]=(0,r.useState)(new Map),[a,o]=(0,r.useState)(!0),[s,c]=(0,r.useState)(null);return(0,r.useEffect)(()=>{if(!e){n(new Map),o(!1);return}let t=i.get(e);if(t){n(t),o(!1);return}o(!0),c(null),l(e).then(()=>{n(i.get(e)??new Map)}).catch(t=>{let r=t instanceof Error?t:Error(`Failed to fetch feature flags`);console.error(`Failed to fetch feature flags for ${e}:`,r),c(r),n(new Map)}).finally(()=>o(!1))},[e]),{flags:t,isLoading:a,error:s}}function p(e){e?i.delete(e):i.clear()}typeof window<`u`&&(window.clearFeatureFlagsCache=p,window.getFeatureFlagsCache=()=>i);function m(e,t){if(!e)return!1;let n=e.indexOf(`.`);if(n>0){let r=e.slice(0,n),a=e.slice(n+1);return t.has(a)?t.get(a)||!1:i.get(r)?.get(a)||!1}if(t.has(e))return t.get(e)||!1;for(let t of i.values())if(t.has(e))return t.get(e)||!1;return!1}function h(e,t){return!(e.required_feature&&!m(e.required_feature,t)||e.exclude_if_feature&&m(e.exclude_if_feature,t))}export{d as a,u as i,l as n,f as o,h as r,p as t};