import"./chunk-O2XFH626.js";import{Cookie as T,Settings as _,Shield as G,BarChart3 as H,Target as K}from"lucide-react";import{useState as w,useEffect as v,useRef as V}from"react";import{Accordion as W,Button as p,BUTTON_VARIANT as h,Card as b,Switch as q,Text as f,cn as J,Stack as i}from"@donotdev/components";import{handleError as Q,useConsent as d,getCookieExamples as y,hasOptionalCookies as X}from"@donotdev/core";import{CONSENT_CATEGORY as l}from"@donotdev/core";import{useTranslation as Z}from"@donotdev/core";import{isClient as $}from"@donotdev/core";import{Fragment as te,jsx as n,jsxs as a}from"react/jsx-runtime";function ee({position:D="bottom",showBranding:ne=!0,brandingText:oe="Powered by DoNotDev Framework",className:O=""}){let r=d("hasConsented"),m=d("categories"),S=d("acceptAll"),B=d("declineAll"),I=d("updateCategory"),g=d("showBanner"),{t}=Z("cookies"),[u,x]=w(!1),[C,c]=w(!r||g),[N,A]=w(m),E=V(null);v(()=>{g&&c(!0)},[g]);let L=Object.keys(m),P=[{id:l.NECESSARY,icon:G,title:t("essentialCookies"),description:t("essentialDescription"),examples:y("necessary")||t("essentialExamples"),locked:!0},{id:l.FUNCTIONAL,icon:T,title:t("functionalCookies"),description:t("functionalDescription"),examples:y("functional")||t("functionalExamples"),legalNotice:t("functionalLegalNotice"),locked:!1},{id:l.ANALYTICS,icon:H,title:t("analyticsCookies"),description:t("analyticsDescription"),examples:y("analytics")||t("analyticsExamples"),locked:!1},{id:l.MARKETING,icon:K,title:t("marketingCookies"),description:t("marketingDescription"),examples:y("marketing")||t("marketingExamples"),locked:!1}].filter(e=>L.includes(e.id)),k=X();if(v(()=>{!r&&!k&&S()},[r,k,S]),v(()=>{let e={...m};if(u){let o=e;Object.keys(o).forEach(s=>{s!==l.NECESSARY&&o[s]===!1&&(o[s]=!0)})}A(e)},[m,u]),v(()=>{if(!$()||r||!C)return;let e=s=>{E.current&&!E.current.contains(s.target)&&c(!1)},o=setTimeout(()=>{document.addEventListener("mousedown",e)},100);return()=>{clearTimeout(o),document.removeEventListener("mousedown",e)}},[r,C]),r&&!g)return null;let R=()=>{try{Object.entries(N).forEach(([e,o])=>{if(e!==l.NECESSARY&&o!==void 0){let s=e;Object.values(l).includes(s)&&I(s,!!o)}}),x(!1),c(!1)}catch(e){Q(e,{userMessage:"Error saving preferences",context:{operation:"save_preferences"},severity:"warning",log:!0,reportToSentry:!1,showNotification:!1})}},U=()=>{S(),c(!1)},z=()=>{B(),c(!1)},F=a(i,{children:[n(f,{as:"p",variant:"muted",level:"small",children:t("cookieBannerDescription")}),k&&n(W,{type:"single",collapsible:!0,value:u?"preferences":"",onValueChange:e=>x(e==="preferences"),items:[{value:"preferences",trigger:a(i,{direction:"row",align:"center",gap:"tight",children:[n(_,{className:"dndev-size-md"}),t("customize")]}),content:n(i,{children:P.map(e=>{let o=e.icon,s=N[e.id]??!1;return n(b,{content:a(i,{direction:"row",align:"center",justify:"between",children:[a(i,{direction:"row",align:"start",gap:"tight",style:{flex:1},children:[n(o,{className:"dndev-size-md",style:{color:"var(--primary)",marginTop:"0.125rem"}}),a(i,{gap:"tight",children:[n(f,{as:"span",level:"body",style:{fontWeight:600},children:e.title}),n(f,{as:"span",variant:"muted",level:"small",children:e.description}),e.examples&&n(f,{as:"span",variant:"muted",level:"small",style:{fontStyle:"italic"},children:e.examples})]})]}),e.locked?n(f,{as:"span",variant:"muted",level:"small",children:t("alwaysOn")}):n(q,{checked:s,onCheckedChange:j=>A(M=>({...M,[e.id]:j}))})]})},e.id)})})}]})]}),Y=u?a(i,{direction:"row",gap:"tight",style:{width:"100%"},children:[n(p,{onClick:R,variant:h.DEFAULT,style:{flex:1},children:t("savePreferences","Save Preferences")}),n(p,{onClick:()=>x(!1),variant:h.OUTLINE,style:{flex:1},children:t("form.cancel","Cancel")})]}):a(i,{direction:"row",gap:"tight",style:{width:"100%"},children:[n(p,{onClick:U,variant:h.DEFAULT,style:{flex:1},children:t("acceptAll")}),k&&n(p,{onClick:z,variant:h.OUTLINE,style:{flex:1},children:t("declineOptional")})]});return a(te,{children:[C&&n(b,{ref:E,className:J("dndev-fixed dndev-z-modal",O),icon:T,title:t("cookiePreferences"),content:F,footer:Y,style:{[D==="top"?"top":"bottom"]:"var(--gap-md)",insetInlineStart:"var(--gap-md)",insetInlineEnd:"var(--gap-md)",maxWidth:"100%",width:"calc(100% - var(--gap-md) * 2)"}}),!C&&!r&&n(p,{onClick:()=>c(!0),className:"dndev-fixed dndev-z-toast",style:{bottom:"var(--gap-md)",insetInlineStart:"var(--gap-md)"},icon:T,title:t("cookiePreferences")})]})}var pe=ee;export{pe as default};
