@e-llm-studio/instant-learning 0.1.1-alpha.63 → 0.1.1-alpha.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/dist/cjs/features/IL-OTJ/ILOTJ.js +1 -1
  2. package/dist/cjs/features/IL-OTJ/ILOTJTest.js +1 -1
  3. package/dist/cjs/features/IL-OTJ/ValueTypeWrappers/TeachMeWrapper.js +1 -1
  4. package/dist/cjs/features/IL-OTJ/_components/ChatComponents/ChatComponent.js +1 -1
  5. package/dist/cjs/features/IL-OTJ/_components/templates/RuleComparison/RuleComparison.js +1 -1
  6. package/dist/cjs/features/LearningManagement/LearningManagement.js +1 -1
  7. package/dist/cjs/features/LearningManagement/LearningTemplateLayout.js +1 -1
  8. package/dist/cjs/features/LearningManagement/RuleContent/ContentLogic.js +1 -1
  9. package/dist/cjs/features/LearningManagement/RuleContent/RuleAccordion.js +1 -1
  10. package/dist/cjs/features/LearningManagement/RuleContent/RuleAccordionHeader.js +1 -1
  11. package/dist/cjs/features/LearningManagement/RuleContent/RuleAccordionShell.js +1 -1
  12. package/dist/cjs/features/LearningManagement/RuleContent/RuleContent.js +1 -1
  13. package/dist/cjs/features/LearningManagement/RuleContent/RuleHeader.js +1 -1
  14. package/dist/cjs/features/LearningManagement/RuleContent/TierCard.js +1 -1
  15. package/dist/cjs/features/LearningManagement/RuleContent/VariantCard.js +1 -1
  16. package/dist/cjs/features/LearningManagement/RuleContent/VariantList.js +1 -1
  17. package/dist/cjs/features/LearningManagement/Sidebar.js +1 -1
  18. package/dist/cjs/features/LearningManagement/SidebarWrapper.js +1 -1
  19. package/dist/cjs/features/LearningManagement/apis/apiConfig.js +1 -1
  20. package/dist/cjs/features/LearningManagement/apis/scopeApis.js +1 -1
  21. package/dist/cjs/features/LearningManagement/components/CreateLearningSetModal.js +1 -1
  22. package/dist/cjs/features/LearningManagement/components/CreateRuleSetLegacyModal.js +1 -1
  23. package/dist/cjs/features/LearningManagement/components/EmptyLearningState.js +1 -1
  24. package/dist/cjs/features/LearningManagement/components/InstantLearningModal.js +1 -1
  25. package/dist/cjs/features/LearningManagement/components/ScopeTree/TreeAddAction.js +1 -1
  26. package/dist/cjs/features/LearningManagement/components/ScopeTree/TreeNode.js +1 -1
  27. package/dist/cjs/features/LearningManagement/components/ScopeTree/TreeSidebar.js +1 -1
  28. package/dist/cjs/features/LearningManagement/components/StationOnboarding/StationAutocomplete.js +1 -1
  29. package/dist/cjs/features/LearningManagement/components/StationOnboarding/StationOnboardModal.js +1 -1
  30. package/dist/cjs/features/LearningManagement/hooks/useLearningTemplateQueryParams.js +1 -1
  31. package/dist/cjs/features/LearningManagement/store/useEnvStore.js +1 -1
  32. package/dist/cjs/features/LearningManagement/store/useLearningTemplateStore.js +1 -1
  33. package/dist/cjs/features/LearningManagement/styles/EmptyRuleState.module.css.js +1 -1
  34. package/dist/cjs/plugins/ValueTypes/Default/DefaultTeachMeRenderer.js +1 -1
  35. package/dist/cjs/plugins/ValueTypes/Guidelines/GuidelinesTeachMeRenderer.js +1 -1
  36. package/dist/features/IL-OTJ/ILOTJ.js +1 -1
  37. package/dist/features/IL-OTJ/ILOTJTest.js +1 -1
  38. package/dist/features/IL-OTJ/ValueTypeWrappers/TeachMeWrapper.js +1 -1
  39. package/dist/features/IL-OTJ/_components/ChatComponents/ChatComponent.js +1 -1
  40. package/dist/features/IL-OTJ/_components/templates/RuleComparison/RuleComparison.js +1 -1
  41. package/dist/features/LearningManagement/LearningManagement.js +1 -1
  42. package/dist/features/LearningManagement/LearningTemplateLayout.js +1 -1
  43. package/dist/features/LearningManagement/RuleContent/ContentLogic.js +1 -1
  44. package/dist/features/LearningManagement/RuleContent/RuleAccordion.js +1 -1
  45. package/dist/features/LearningManagement/RuleContent/RuleAccordionHeader.js +1 -1
  46. package/dist/features/LearningManagement/RuleContent/RuleAccordionShell.js +1 -1
  47. package/dist/features/LearningManagement/RuleContent/RuleContent.js +1 -1
  48. package/dist/features/LearningManagement/RuleContent/RuleHeader.js +1 -1
  49. package/dist/features/LearningManagement/RuleContent/TierCard.js +1 -1
  50. package/dist/features/LearningManagement/RuleContent/VariantCard.js +1 -1
  51. package/dist/features/LearningManagement/RuleContent/VariantList.js +1 -1
  52. package/dist/features/LearningManagement/Sidebar.js +1 -1
  53. package/dist/features/LearningManagement/SidebarWrapper.js +1 -1
  54. package/dist/features/LearningManagement/apis/apiConfig.js +1 -1
  55. package/dist/features/LearningManagement/apis/scopeApis.js +1 -1
  56. package/dist/features/LearningManagement/components/CreateLearningSetModal.js +1 -1
  57. package/dist/features/LearningManagement/components/CreateRuleSetLegacyModal.js +1 -1
  58. package/dist/features/LearningManagement/components/EmptyLearningState.js +1 -1
  59. package/dist/features/LearningManagement/components/InstantLearningModal.js +1 -1
  60. package/dist/features/LearningManagement/components/ScopeTree/TreeAddAction.js +1 -1
  61. package/dist/features/LearningManagement/components/ScopeTree/TreeNode.js +1 -1
  62. package/dist/features/LearningManagement/components/ScopeTree/TreeSidebar.js +1 -1
  63. package/dist/features/LearningManagement/components/StationOnboarding/StationAutocomplete.js +1 -1
  64. package/dist/features/LearningManagement/components/StationOnboarding/StationOnboardModal.js +1 -1
  65. package/dist/features/LearningManagement/hooks/useLearningTemplateQueryParams.js +1 -1
  66. package/dist/features/LearningManagement/store/useEnvStore.js +1 -1
  67. package/dist/features/LearningManagement/store/useLearningTemplateStore.js +1 -1
  68. package/dist/features/LearningManagement/styles/EmptyRuleState.module.css.js +1 -1
  69. package/dist/plugins/ValueTypes/Default/DefaultTeachMeRenderer.js +1 -1
  70. package/dist/plugins/ValueTypes/Guidelines/GuidelinesTeachMeRenderer.js +1 -1
  71. package/dist/types/src/features/IL-OTJ/ILOTJ.d.ts +1 -1
  72. package/dist/types/src/features/IL-OTJ/ILOTJ.d.ts.map +1 -1
  73. package/dist/types/src/features/IL-OTJ/ILOTJTest.d.ts.map +1 -1
  74. package/dist/types/src/features/IL-OTJ/ValueTypeWrappers/TeachMeWrapper.d.ts +0 -7
  75. package/dist/types/src/features/IL-OTJ/ValueTypeWrappers/TeachMeWrapper.d.ts.map +1 -1
  76. package/dist/types/src/features/IL-OTJ/_components/ChatComponents/ChatComponent.d.ts +1 -1
  77. package/dist/types/src/features/IL-OTJ/_components/ChatComponents/ChatComponent.d.ts.map +1 -1
  78. package/dist/types/src/features/IL-OTJ/_components/templates/RuleComparison/RuleComparison.d.ts +41 -6
  79. package/dist/types/src/features/IL-OTJ/_components/templates/RuleComparison/RuleComparison.d.ts.map +1 -1
  80. package/dist/types/src/features/LearningManagement/LearningManagement.d.ts +1 -3
  81. package/dist/types/src/features/LearningManagement/LearningManagement.d.ts.map +1 -1
  82. package/dist/types/src/features/LearningManagement/LearningManagementTest.d.ts.map +1 -1
  83. package/dist/types/src/features/LearningManagement/LearningTemplateLayout.d.ts +0 -3
  84. package/dist/types/src/features/LearningManagement/LearningTemplateLayout.d.ts.map +1 -1
  85. package/dist/types/src/features/LearningManagement/RuleContent/ContentLogic.d.ts.map +1 -1
  86. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordion.d.ts +0 -2
  87. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordion.d.ts.map +1 -1
  88. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordionHeader.d.ts +0 -2
  89. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordionHeader.d.ts.map +1 -1
  90. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordionShell.d.ts +0 -2
  91. package/dist/types/src/features/LearningManagement/RuleContent/RuleAccordionShell.d.ts.map +1 -1
  92. package/dist/types/src/features/LearningManagement/RuleContent/RuleContent.d.ts.map +1 -1
  93. package/dist/types/src/features/LearningManagement/RuleContent/RuleHeader.d.ts +0 -2
  94. package/dist/types/src/features/LearningManagement/RuleContent/RuleHeader.d.ts.map +1 -1
  95. package/dist/types/src/features/LearningManagement/RuleContent/TierCard.d.ts.map +1 -1
  96. package/dist/types/src/features/LearningManagement/RuleContent/VariantCard.d.ts +0 -4
  97. package/dist/types/src/features/LearningManagement/RuleContent/VariantCard.d.ts.map +1 -1
  98. package/dist/types/src/features/LearningManagement/RuleContent/VariantList.d.ts +0 -2
  99. package/dist/types/src/features/LearningManagement/RuleContent/VariantList.d.ts.map +1 -1
  100. package/dist/types/src/features/LearningManagement/Sidebar.d.ts +0 -3
  101. package/dist/types/src/features/LearningManagement/Sidebar.d.ts.map +1 -1
  102. package/dist/types/src/features/LearningManagement/SidebarWrapper.d.ts +0 -3
  103. package/dist/types/src/features/LearningManagement/SidebarWrapper.d.ts.map +1 -1
  104. package/dist/types/src/features/LearningManagement/apis/apiConfig.d.ts +0 -6
  105. package/dist/types/src/features/LearningManagement/apis/apiConfig.d.ts.map +1 -1
  106. package/dist/types/src/features/LearningManagement/apis/scopeApis.d.ts +0 -70
  107. package/dist/types/src/features/LearningManagement/apis/scopeApis.d.ts.map +1 -1
  108. package/dist/types/src/features/LearningManagement/components/CreateLearningSetModal.d.ts.map +1 -1
  109. package/dist/types/src/features/LearningManagement/components/CreateRuleSetLegacyModal.d.ts +0 -1
  110. package/dist/types/src/features/LearningManagement/components/CreateRuleSetLegacyModal.d.ts.map +1 -1
  111. package/dist/types/src/features/LearningManagement/components/EmptyLearningState.d.ts +0 -10
  112. package/dist/types/src/features/LearningManagement/components/EmptyLearningState.d.ts.map +1 -1
  113. package/dist/types/src/features/LearningManagement/components/InstantLearningModal.d.ts.map +1 -1
  114. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeAddAction.d.ts +0 -2
  115. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeAddAction.d.ts.map +1 -1
  116. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeMenu.d.ts.map +1 -1
  117. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeNode.d.ts +0 -3
  118. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeNode.d.ts.map +1 -1
  119. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeSidebar.d.ts +0 -3
  120. package/dist/types/src/features/LearningManagement/components/ScopeTree/TreeSidebar.d.ts.map +1 -1
  121. package/dist/types/src/features/LearningManagement/components/StationOnboarding/StationAutocomplete.d.ts +0 -1
  122. package/dist/types/src/features/LearningManagement/components/StationOnboarding/StationAutocomplete.d.ts.map +1 -1
  123. package/dist/types/src/features/LearningManagement/components/StationOnboarding/StationOnboardModal.d.ts.map +1 -1
  124. package/dist/types/src/features/LearningManagement/hooks/useLearningTemplateQueryParams.d.ts +0 -6
  125. package/dist/types/src/features/LearningManagement/hooks/useLearningTemplateQueryParams.d.ts.map +1 -1
  126. package/dist/types/src/features/LearningManagement/store/useEnvStore.d.ts +0 -13
  127. package/dist/types/src/features/LearningManagement/store/useEnvStore.d.ts.map +1 -1
  128. package/dist/types/src/features/LearningManagement/store/useLearningTemplateStore.d.ts +2 -6
  129. package/dist/types/src/features/LearningManagement/store/useLearningTemplateStore.d.ts.map +1 -1
  130. package/dist/types/src/features/LearningManagement/types.d.ts +1 -2
  131. package/dist/types/src/features/LearningManagement/types.d.ts.map +1 -1
  132. package/dist/types/src/routes/Routes.d.ts +1 -1
  133. package/dist/types/src/routes/Routes.d.ts.map +1 -1
  134. package/package.json +1 -3
  135. package/dist/cjs/features/LearningManagement/assets/Search-result(add new).svg.js +0 -2
  136. package/dist/cjs/features/LearningManagement/assets/Search-result(add new).svg.js.map +0 -1
  137. package/dist/cjs/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.js +0 -2
  138. package/dist/cjs/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.js.map +0 -1
  139. package/dist/features/LearningManagement/assets/Search-result(add new).svg.js +0 -2
  140. package/dist/features/LearningManagement/assets/Search-result(add new).svg.js.map +0 -1
  141. package/dist/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.js +0 -2
  142. package/dist/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.js.map +0 -1
  143. package/dist/types/src/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.d.ts +0 -12
  144. package/dist/types/src/features/LearningManagement/components/StationOnboarding/BackwardStationOnboardModal.d.ts.map +0 -1
@@ -1,2 +1,2 @@
1
- import{__assign as e,__spreadArray as n,__awaiter as t,__generator as i}from"tslib";import{jsxs as r,jsx as a,Fragment as o}from"react/jsx-runtime";import l,{useState as s,useRef as u,useEffect as d,useCallback as c,useMemo as v}from"react";import{AlertTriangle as _,Loader as p,Check as f}from"lucide-react";import{Toast as g}from"primereact/toast";import{v4 as m}from"uuid";import{saveInstantLearningQuestion as w,publishFeedback as y,updateVariant as h,fetchRulesByQuestionId as b,submitChatFeedback as T}from"../apis/scopeApis.js";import A from"../public/ThinkingBulbIcon.gif.js";import{useLearningTemplateStore as M}from"../store/useLearningTemplateStore.js";import I from"../styles/InstantLearningModal.module.css.js";import k from"../../IL-OTJ/ILOTJ.js";import"../../IL-OTJ/_components/atoms/Textarea/Textarea.js";import"../../IL-OTJ/_components/InputDataReasoningCard.js";import"../../IL-OTJ/_components/TooltipComponent.js";import{TemplateList as S}from"../../IL-OTJ/_components/templates/TemplatesMap/templatesMap.js";import{extractAndParseWithProperty as N,parseValidationData as C,extractAndParseResponse as E,extractVariantIds as x}from"../../IL-OTJ/helpers/utils.js";import{useCitations as O}from"../../IL-OTJ/store/CitationsContext.js";import"@mui/material";import"@mui/material/Tooltip";import"react-markdown";import"rehype-raw";import"axios";import"@e-llm-studio/streaming-response";import"@e-llm-studio/watch-me-work";import"../../IL-OTJ/_components/ChatComponents/ChatHeader.js";import"remark-gfm";import"../../IL-OTJ/_components/MarkdownComponents/MarkdownElements.js";import"../../IL-OTJ/_components/CitationLink.js";import"@e-llm-studio/citation";import"@viswa-test/test-citation";import"../LearningManagement.js";import L from"../store/useAuthStore.js";import{useConfigStore as R}from"../store/useConfigStore.js";import{IL_CONFIG_CONSTANTS as D}from"../config.js";var j=function(){return a("img",{src:A,alt:"Thinking...",width:"20",height:"20"})},V="INITIAL",P="SUCCESS",G="FAILURE",F="IN_PROGRESS",q=function(e){return"string"!=typeof e?"":e.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,"").trim()},U=function(e){if(!e)return!1;var n=e.toLowerCase().trim();return"default"===n||"default_variant"===n},J=function(e){return e.toLowerCase().replace(/ /g,"_").replace(/tier(\d+)/i,"tier_$1")},H=function(A){var H,B,W,Y,K,Q,z=A.isOpen,Z=A.onClose,$=A.initialScope,X=void 0===$?"global":$,ee=A.ruleSet,ne=A.editingRule,te=A.editingTier,ie=A.editingVariants,re=A.focusedVariantIndex,ae=A.setFocusedVariantIndex,oe=A.triggerAddVariant,le=A.triggerFixGaps,se=A.copiedRule,ue=A.copiedSourceTier,de=A.copiedTargetTier,ce=A.initialDraftMessage,ve=A.initialChatMessage,_e=A.applicationIntegrationId;A.modeId;var pe=s({station:X,tiers:["Tier 1"]}),fe=pe[0],ge=pe[1],me=s(V),we=me[0],ye=me[1],he=s(!1),be=he[0],Te=he[1],Ae=s("Thank you teacher for teaching me, your rule has been saved successfully."),Me=Ae[0];Ae[1];var Ie=s(""),ke=Ie[0],Se=Ie[1],Ne=s(!1),Ce=Ne[0],Ee=Ne[1],xe=u(null),Oe=s(!1),Le=Oe[0],Re=Oe[1],De=u(!1),je=M().tiers,Ve=l.useRef(new Set),Pe=s(null);Pe[0];var Ge=Pe[1],Fe=s(null);Fe[0];var qe=Fe[1],Ue=s(null);Ue[0];var Je=Ue[1],He=u({}),Be=u(new Map),We=u(""),Ye=u(!1),Ke=s(0),Qe=Ke[0],ze=Ke[1],Ze=s(!1),$e=Ze[0],Xe=Ze[1],en=u(null),nn=u(0),tn=s(null),rn=tn[0],an=tn[1],on=s([]),ln=on[0],sn=on[1],un=s(null),dn=un[0],cn=un[1],vn=s(null),_n=vn[0],pn=vn[1],fn=s(!1),gn=fn[0],mn=fn[1],wn=u({}),yn=u([]),hn=u(!1),bn=s(!1),Tn=bn[0],An=bn[1],Mn=u(!1),In=u(new Set),kn=s(null),Sn=kn[0],Nn=kn[1],Cn=s(null),En=Cn[0],xn=Cn[1],On=s(null),Ln=On[0],Rn=On[1],Dn=u(null),jn=u(null),Vn=u(null),Pn=u({}),Gn=u([]),Fn=u([]),qn=u({}),Un=R(),Jn=Un.configuration,Hn=Un.ilLabels;d(function(){Sn||xn(null)},[Sn]),d(function(){var e;if(z&&ne){Dn.current=ne.ruleId;var n=null===(e=null==ie?void 0:ie[re||0])||void 0===e?void 0:e.variantId;Rn(n||null)}},[z,ne,ie,re]),d(function(){z&&ge(function(n){return e(e({},n),{station:X})})},[z,X]),d(function(){console.log("triggered",Ln),console.log(gt,"structuredAIResponse"),mt(function(n){return Array.isArray(n)?n.map(function(n){return e(e({},n),{is_readOnly:n.variant_id!==Ln&&n.new_variant_id!==Ln})}):n}),At(function(n){return Array.isArray(n)?n.map(function(n){return e(e({},n),{is_readOnly:n.variant_id!==Ln&&n.new_variant_id!==Ln})}):n})},[Ln]),d(function(){var n;if(!z)return Mr(),void(hn.current=!1);if(!hn.current){var t=ie,i=t&&t.length>0,r=null!=ne;if(i||r){if((null==ne?void 0:ne.ruleTitle)&&tt(ne.ruleTitle),te&&(ge(function(n){return e(e({},n),{tiers:[te]})}),jn.current=te.toLowerCase().replace(/ /g,"_")),i){var a=te?J(te):"tier_1",o=new Map,l=new Map,s=new Map,u=new Map,d=new Map,c=new Map,v=[];t.forEach(function(e,n){u.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];c.set(n,i),v.push(e.variantId||e.variant_id||"")}),o.set(a,u),l.set(a,d),s.set(a,c);var _=null===(n=null==t?void 0:t[re||0])||void 0===n?void 0:n.variantId,p=t.map(function(e,n){var t,i,r=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config;return{variant_type:e.variantType||"variant",title:(null==ne?void 0:ne.ruleTitle)||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[a],scheduling_config:r||{},variant_id:e.variantId,question_id:(null==ne?void 0:ne.ruleId)||"",tier_id:a.replace("tier_",""),is_readOnly:e.variantId!==_,link_id:e.link_id||"",is_enabled:null===(i=e.is_enabled)||void 0===i||i,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}});mt(p),mt(p),At(p),ht(function(e){var n=new Map(e);return n.set(a,JSON.parse(JSON.stringify(p))),n});var f=new Map;t.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,v,_,p,g=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,m=null!==(a=null!==(i=null==g?void 0:g.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==g?void 0:g.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,w=null!==(s=null!==(o=null==g?void 0:g.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==g?void 0:g.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;f.set(n,{order_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},advertiser_rules:{enabled:w>0,global_min_separation_seconds:Math.floor(w/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==g?void 0:g.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==g?void 0:g.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(p=null!==(_=null===(v=null==g?void 0:g.break_code_rules)||void 0===v?void 0:v.respect_break_code)&&void 0!==_?_:null==g?void 0:g.respect_break_code)&&void 0!==p&&p},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[a,f]])),st(o),ct(l),pt(s)}else if(r&&ne){var g=ne.ruleDesc;if(g&&st(new Map([["tier_1",new Map([[0,g]])]])),ne.whenToApply){var m=ne.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,n){return{id:"edit-wta-".concat(n),text:e,reasoning:""}});ct(new Map([["tier_1",new Map([[0,m]])]]))}if(ne.whenNotToApply){var w=ne.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,n){return{id:"edit-wnta-".concat(n),text:e,reasoning:""}});pt(new Map([["tier_1",new Map([[0,w]])]]))}}}else if(se&&ue&&de){tt(se.ruleTitle||""),ge(function(n){return e(e({},n),{tiers:[de]})}),jn.current=J(de);var y=J(ue),h=se.variants||[],b=(o=new Map,l=new Map,s=new Map,new Map),T=new Map,A=new Map;h.forEach(function(e,n){b.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e,t){return{id:"copy-wta-".concat(n,"-").concat(t),text:e.trim(),reasoning:""}}):[];T.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e,t){return{id:"copy-wnta-".concat(n,"-").concat(t),text:e.trim(),reasoning:""}}):[];A.set(n,i)}),o.set(y,b),l.set(y,T),s.set(y,A),st(o),ct(l),pt(s);var M=h.map(function(e,n){var t,i,r=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config;return{variant_type:e.variantType||"variant",title:se.ruleTitle||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[ue],scheduling_config:r||{},variant_id:e.variantId,question_id:se.ruleId||"",tier_id:y.replace("tier_",""),is_readOnly:!0,link_id:e.link_id||"",is_enabled:null===(i=e.is_enabled)||void 0===i||i,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"copy"}});mt(M),At(M),ht(function(e){var n=new Map(e);return n.set(y,JSON.parse(JSON.stringify(M))),n});var I=new Map;h.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,v,_,p,f=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,g=null!==(a=null!==(i=null==f?void 0:f.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==f?void 0:f.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,m=null!==(s=null!==(o=null==f?void 0:f.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==f?void 0:f.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;I.set(n,{order_rules:{enabled:g>0,global_min_separation_seconds:Math.floor(g/60)},advertiser_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==f?void 0:f.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==f?void 0:f.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(p=null!==(_=null===(v=null==f?void 0:f.break_code_rules)||void 0===v?void 0:v.respect_break_code)&&void 0!==_?_:null==f?void 0:f.respect_break_code)&&void 0!==p&&p},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[y,I]]))}}},[z,ne,ie,te,se,ue,de]),d(function(){if(z){if(console.log("[Accordion Init] editingTier:",te,"focusedVariantIndex:",re),te&&ge(function(n){return e(e({},n),{tiers:[te]})}),gn)return console.log("[Accordion] isAddingNewVariant mode — closing all variant accordions"),void Ot(void 0);if(oe){console.log("[Accordion] triggerAddVariant mode — not opening any variant on RHS"),Ot(void 0);var n=(te?J(te):"tier_1").replace(/_/g," ");Dt("I want to add a new variant to ".concat(n))}else null!=re?(console.log("[Accordion] Opening SPECIFIC variant index ".concat(re," (from VariantCard)")),Ot(re),ti(function(e){return e+1})):(ne||ie&&ie.length>0)&&(console.log("[Accordion] Opening DEFAULT/1st variant (index 0) for RuleHeader edit"),Ot(0),ti(function(e){return e+1}))}else Ot(void 0)},[z,te,re,ne,ie,oe,gn]),d(function(){z&&le&&ne&&null!==re&&Dt("I want to fix the gaps")},[z,le,ne,re]);var Bn={order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1},is_default_enabled:!1},Wn=s(new Map([["tier_1",new Map([[0,Bn]])],["tier_2",new Map([[0,Bn]])]])),Yn=Wn[0],Kn=Wn[1],Qn=s(new Set([])),zn=Qn[0],Zn=Qn[1],$n=s("");$n[0];var Xn=$n[1],et=s(""),nt=et[0],tt=et[1],it=s(new Map([["tier_1",new Map],["tier_2",new Map]])),rt=it[0],at=it[1],ot=s(new Map([["tier_1",new Map],["tier_2",new Map]])),lt=ot[0],st=ot[1],ut=s(new Map([["tier_1",new Map],["tier_2",new Map]])),dt=ut[0],ct=ut[1],vt=s(new Map([["tier_1",new Map],["tier_2",new Map]])),_t=vt[0],pt=vt[1],ft=s([]),gt=ft[0],mt=ft[1],wt=s(new Map),yt=wt[0],ht=wt[1],bt=s([]),Tt=bt[0],At=bt[1],Mt=s(!1),It=Mt[0],kt=Mt[1],St=s([]),Nt=St[0],Ct=St[1],Et=s(void 0),xt=Et[0],Ot=Et[1],Lt=s(""),Rt=Lt[0],Dt=Lt[1],jt=s(null),Vt=jt[0],Pt=jt[1],Gt=s([]),Ft=Gt[0],qt=Gt[1],Ut=s([]),Jt=Ut[0],Ht=Ut[1],Bt=s({}),Wt=Bt[0],Yt=Bt[1],Kt=s(new Map),Qt=Kt[0],zt=Kt[1],Zt=s(new Set),$t=Zt[0],Xt=Zt[1],ei=s(0),ni=ei[0],ti=ei[1],ii=s({ruleTitle:"",ruleDescription:"",scopeData:"",whenToApply:"",whenNotToApply:"",configData:""}),ri=ii[0],ai=ii[1],oi=s([]),li=oi[0],si=oi[1],ui=s(""),di=ui[0],ci=ui[1],vi=s({}),_i=vi[0],pi=vi[1],fi=s(!1),gi=fi[0],mi=fi[1],wi=u(null),yi=u(null),hi=u(-1),bi=u(null);d(function(){Fn.current=gt},[gt]),d(function(){var n=new Set,t=console.warn;return console.warn=function(){for(var i,o=[],l=0;l<arguments.length;l++)o[l]=arguments[l];t.apply(void 0,o);var s=o[0];if("string"==typeof s&&s.includes("Failed to parse JSON for property=")){var u=s.match(/warnId="([^"]+)"/),d=null==u?void 0:u[1];if(d&&n.has(d))return;d&&n.add(d),null===(i=bi.current)||void 0===i||i.show({severity:"warn",sticky:!1,life:5e3,className:I.toastWarning,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(_,{className:I.iconWarning}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleWarning)},{children:"Parsing Issue"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageWarning)},{children:"The AI response couldn't be parsed correctly. Please try rephrasing your input or retry."}))]}))})}},function(){console.warn=t}},[]);var Ti=s(!1),Ai=Ti[0],Mi=Ti[1],Ii=s(!1),ki=Ii[0],Si=Ii[1],Ni=s({}),Ci=Ni[0],Ei=Ni[1],xi=s(""),Oi=xi[0],Li=xi[1],Ri=s(!0),Di=Ri[0],ji=Ri[1],Vi=s(!1),Pi=Vi[0],Gi=Vi[1],Fi=s(!1),qi=Fi[0],Ui=Fi[1],Ji=s(""),Hi=Ji[0],Bi=Ji[1],Wi=s(""),Yi=Wi[0],Ki=Wi[1],Qi=s(""),zi=Qi[0],Zi=Qi[1],$i=s(null),Xi=$i[0],er=$i[1],nr=s(null),tr=nr[0],ir=nr[1],rr=s(null),ar=rr[0],or=rr[1],lr=L.getState().user,sr=O(),ur=sr.dispatch,dr=sr.getLatestReasoning;d(function(){console.log("Structured AI Response updated:",gt)},[gt]),d(function(){console.log("isCloseModal AI Response updated:",Di)},[Di]),d(function(){!1===gi?(Si(!1),Bi(""),Ki("")):(Ee(!1),Se(""))},[gi]),d(function(){null!=xt&&ie&&!ie[xt]&&er(null)},[xt,ie]),d(function(){if(Le&&!De.current){De.current=!0,console.log("UseEffect triggered: Sending save message to chat agent");Dt("Save this Learning"),Re(!1),setTimeout(function(){De.current=!1,Dt("")},2e3)}},[Le]);var cr=c(function(e,n){ln.some(function(n){return n.id===e})?(Ui(!0),cn(e),pn(n),setTimeout(function(){pn(null),cn(null)},6e3)):console.warn("Variant ID ".concat(e," not found in comparison variants."))},[ln]),vr=v(function(){var e=qi&&Qe>0;return console.log("[Loader Debug] showComparison: ".concat(qi,", activeRequests: ").concat(Qe,", Result: ").concat(e)),e},[qi,Qe]),_r=c(function(n,t,i){var r,a=void 0!==(null===(r=null==i?void 0:i.target)||void 0===r?void 0:r.value)?i.target.value:i;st(function(e){var i=new Map(e),r=new Map(i.get(n));return r.set(t,a),i.set(n,r),i}),Ei(function(i){if(i["ruleDescription-".concat(n,"-").concat(t)]){var r=e({},i);return delete r["ruleDescription-".concat(n,"-").concat(t)],r}return i})},[]),pr=c(function(t,i){ct(function(e){var r=new Map(e),a=new Map(r.get(t)),o=a.get(i)||[];return a.set(i,n(n([],o,!0),[{id:Date.now(),text:"",reasoning:""}],!1)),r.set(t,a),r}),Ei(function(n){if(n["whenToApply-".concat(t,"-").concat(i)]){var r=e({},n);return delete r["whenToApply-".concat(t,"-").concat(i)],r}return n})},[]),fr=c(function(e,n,t){ct(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(n)||[];return a.set(n,o.filter(function(e){return e.id!==t})),r.set(e,a),r})},[]),gr=c(function(n,t,i,r){ct(function(a){var o=new Map(a),l=new Map(o.get(n)),s=l.get(t)||[];return l.set(t,s.map(function(n){return String(n.id)===String(i)?e(e({},n),{text:r}):n})),o.set(n,l),o}),Ei(function(i){if(i["whenToApply-".concat(n,"-").concat(t)]){var r=e({},i);return delete r["whenToApply-".concat(n,"-").concat(t)],r}return i})},[]),mr=c(function(e,t){pt(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(t)||[];return a.set(t,n(n([],o,!0),[{id:Date.now(),text:"",reasoning:""}],!1)),r.set(e,a),r})},[]),wr=c(function(e,n,t){pt(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(n)||[];return a.set(n,o.filter(function(e){return e.id!==t})),r.set(e,a),r})},[]),yr=c(function(n,t,i,r){pt(function(a){var o=new Map(a),l=new Map(o.get(n)),s=l.get(t)||[];return l.set(t,s.map(function(n){return String(n.id)===String(i)?e(e({},n),{text:r}):n})),o.set(n,l),o})},[]),hr=c(function(e){var n=e.replace(/_/g," ");mn(!0),null==ae||ae(null),Ot(void 0),Dt("I want to add a new variant to ".concat(n))},[]),br=function(n,t,i){var r="".concat(n,"_").concat(t);console.log("Highlighting ".concat(i," at ").concat(r)),zt(function(n){var t,a=new Map(n),o=a.get(r)||{};return a.set(r,e(e({},o),((t={})[i]=!0,t))),a}),setTimeout(function(){zt(function(n){var t,a=new Map(n),o=a.get(r)||{};return a.set(r,e(e({},o),((t={})[i]=!1,t))),a})},1500)},Tr=c(function(n,t){ge(function(i){var r;return e(e({},i),((r={})[n]=t,r))}),Ei(function(t){if(t[n]){var i=e({},t);return delete i[n],i}return t})},[]),Ar=c(function(n,t,i,r){at(function(e){var r=new Map(e),a=new Map(r.get(n)||new Map),o=a.get(t)||new Set;return o.add(i),a.set(t,o),r.set(n,a),r}),Kn(function(a){var o,l,s,u=new Map(a),d=new Map(u.get(n)||new Map),c=d.get(t)||Bn;switch(i){case"order_rules":case"advertiser_rules":s="boolean"==typeof r?e(e({},c),((o={})[i]=e(e({},c[i]),{enabled:r}),o)):"object"==typeof r&&null!==r?e(e({},c),((l={})[i]={enabled:r.enabled,global_min_separation_seconds:r.global_min_separation_seconds},l)):c;break;case"inventory_rules":s=e(e({},c),{inventory_rules:{respect_inventory_code:"boolean"==typeof r?r:r.respect_inventory_code}});break;case"break_code_rules":s=e(e({},c),{break_code_rules:{respect_break_code:"boolean"==typeof r?r:r.respect_break_code}});break;case"is_default_enabled":s=e(e({},c),{is_default_enabled:"boolean"==typeof r?r:!!r});break;default:s=c}return d.set(t,s),u.set(n,d),u})},[]);d(function(){gi&&(Ui(!1),pn(null),cn(null))},[gi]);var Mr=c(function(n){var t;if(void 0===n&&(n=!1),n||(ur({type:"RESET"}),wi.current=null,mt([]),Ct([]),si([]),pi({}),Ui(!1),Ve.current.clear()),tt(""),qt([]),mn(!1),We.current="",Zi(""),ir(null),Ht([]),ge({station:"",tiers:[]}),ze(0),Xe(!1),or(null),st(new Map([["tier_1",new Map],["tier_2",new Map]])),ct(new Map([["tier_1",new Map],["tier_2",new Map]])),pt(new Map([["tier_1",new Map],["tier_2",new Map]])),Kn(new Map([["tier_1",new Map([[0,Bn]])],["tier_2",new Map([[0,Bn]])]])),at(new Map([["tier_1",new Map],["tier_2",new Map]])),Xn(""),Ot(0),ti(0),ge({station:X,tiers:["Tier 1"]}),n||Yt({}),at(new Map([["tier_1",new Map],["tier_2",new Map]])),Dt(""),ai({ruleTitle:"",ruleDescription:"",scopeData:"",whenToApply:"",whenNotToApply:"",configData:""}),Ei({}),Li(""),ji(!0),Ge(null),qe(null),ht(new Map),At([]),An(!1),Je(null),yi.current=null,Ye.current=!1,wn.current={},In.current.clear(),Dn.current=null,jn.current=null,Rn(null),Se(""),ye(V),Te(!1),Ee(!1),Re(!1),an(null),sn([]),cn(null),pn(null),Nn(null),kt(!1),Pt(null),Bi(""),Ki(""),Si(!1),Zn(new Set),De.current=!1,Be.current.clear(),nn.current=0,en.current=null,Vn.current=null,hi.current=-1,Mn.current=!1,hn.current=!1,ne){ne.ruleTitle&&tt(ne.ruleTitle),te&&(ge(function(n){return e(e({},n),{tiers:[te]})}),jn.current=te.toLowerCase().replace(/ /g,"_")),Dn.current=ne.ruleId;var i=null===(t=null==ie?void 0:ie[re||0])||void 0===t?void 0:t.variantId;if(Rn(i||null),ie&&ie.length>0){var r=te?J(te):"tier_1",a=new Map,o=new Map,l=new Map,s=new Map,u=new Map,d=new Map;ie.forEach(function(e,n){s.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];u.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,i)}),a.set(r,s),o.set(r,u),l.set(r,d),st(a),ct(o),pt(l);var c=ie.map(function(e){var n,t,a=null===(n=e.configuration)||void 0===n?void 0:n.scheduling_config;return{variant_type:e.variantType||"variant",title:ne.ruleTitle||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[r],scheduling_config:a||{},variant_id:e.variantId,question_id:ne.ruleId||"",tier_id:r.replace("tier_",""),is_readOnly:e.variantId!==i,link_id:e.link_id||"",is_enabled:null===(t=e.is_enabled)||void 0===t||t,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}});mt(c),At(c),ht(function(e){var n=new Map(e);return n.set(r,JSON.parse(JSON.stringify(c))),n});var v=new Map;ie.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,_,p,f,g=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,m=null!==(a=null!==(i=null==g?void 0:g.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==g?void 0:g.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,w=null!==(s=null!==(o=null==g?void 0:g.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==g?void 0:g.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;v.set(n,{order_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},advertiser_rules:{enabled:w>0,global_min_separation_seconds:Math.floor(w/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==g?void 0:g.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==g?void 0:g.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(f=null!==(p=null===(_=null==g?void 0:g.break_code_rules)||void 0===_?void 0:_.respect_break_code)&&void 0!==p?p:null==g?void 0:g.respect_break_code)&&void 0!==f&&f},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[r,v]])),Ot(null!=re?re:0)}}},[ur,X,ne,te,ie,re]),Ir=function(e){if(!e||0===e.length)return"variant 1";var n=e.filter(function(e){var n=e.variant_type||e.variantType||"";return""!==n&&null!==n});return"variant ".concat(n.length+1)},kr=c(function(){console.log("variantIds",Ft),Ui(!0)},[Ft]),Sr={"Let me connect the dots for you...":function(e){if("string"==typeof e){var n=e.match(/<!--\s*(\{[^}]*"variant_index"[^}]*\})\s*-->/);if(n)try{var t=JSON.parse(n[1]),i=parseInt(t.variant_index,10);if(!isNaN(i)&&i>0){var r=i-1;hi.current!==r&&(console.log("Opening variant accordion: AI index ".concat(i," → 0-based index ").concat(r)),Ot(r),hi.current=r)}}catch(e){console.error("Failed to parse variant_index from HTML comment:",e)}}},"Crafted Instant Learning with C.A.R.E.":function(t){var i,r=N(t),a=N(t,"general_details");console.log("--- [CARE] Staging Initial Structure ---",r);var o=N(t,"critical_gaps");if(console.log("isCriticalGap",o),Xe(!!o),r){if(r.variant_type&&!("is_variant"in r)){var l=null===(i=Cr(r.variant_type||"").toLowerCase().match(/^(default_variant|default|variant)/i))||void 0===i?void 0:i[0];r.variant_type=U(l)?"Default":"variant",r.isDefault=U(l),console.log("parsed.variant_type",r.variant_type)}var s=r.scope&&r.scope.length>0?String(r.scope[0]):null,u=s?Cr(s).toLowerCase().trim().replace(/ /g,"_"):null,d=Boolean(u&&jn.current!==u);if(d&&u){var c=jn.current;console.log("[Tier Switch Detected]",jn.current,"→",u),jn.current=u,Dn.current=null;var v=(Fn.current||[]).findIndex(function(e){return!0===(null==e?void 0:e.is_new_variant)});-1===v&&void 0!==xt&&(v=xt),c&&-1!==v&&(console.log("[Tier Switch] Stripping draft variant at index ".concat(v," from ").concat(c)),st(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(v),n.set(c,t),n}),ct(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(v),n.set(c,t),n}),pt(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(v),n.set(c,t),n}),Kn(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(v),n.set(c,t),n}),mt(function(t){if(!Array.isArray(t))return t;var i=n([],t,!0);return i[v]&&(i[v]=e(e({},i[v]),{scope:[u],tier_id:u.replace("tier_","")})),Fn.current=i,i})),hi.current=-1;var _=u.replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()});ge(function(n){return e(e({},n),{tiers:[_]})})}Pt(r),Ge({Updated_Fields_Response:r,General_Response:a}),(null==a?void 0:a.variant_id)&&"new_variant_id"===(null==a?void 0:a.variant_id)&&(a.variant_id=null),(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&oa(a,r,d),(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&Dn.current&&Ln?mt(function(n){if(!Array.isArray(n))return n;var t=n.map(function(n){return(null==n?void 0:n.variant_id)&&n.variant_id===Ln?e(e({},n),r):n}),i=n.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===Ln});return console.log(i,"targetIndexif"),-1!==i&&setTimeout(function(){console.log("targetIndex",i),Ot(i),ti(function(e){return e+1}),hi.current=i},0),t}):(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&Dn.current&&!Ln?mt(function(t){var i;if(Array.isArray(t)){var a=t.findIndex(function(e){return!0===e.is_new_variant});if(-1!==a)setTimeout(function(){Ot(a),ti(function(e){return e+1}),hi.current=a},0),i=t.map(function(n,t){return t===a?e(e(e({},n),r),{is_new_variant:!0,is_variant:!0}):n});else{var o=t.length;setTimeout(function(){Ot(o),ti(function(e){return e+1}),hi.current=o},0),i=n(n([],t,!0),[e(e({},r),{is_new_variant:!0})],!1)}}else setTimeout(function(){Ot(0),ti(function(e){return e+1}),hi.current=0},0),i=[e(e({},r),{is_new_variant:!0})];return Fn.current=i,i}):(null==a?void 0:a.is_interpretation_ready)&&(mt([r]),setTimeout(function(){Ot(0),ti(function(e){return e+1}),hi.current=0},0)),void 0!==r.is_user_accepted_risk&&mt(function(t){var i=!(!ne&&!ie),a=Array.isArray(t)?n([],t,!0):[];if(i&&0===a.length&&ie&&ie.length>0&&(a=ie.map(function(e,n){var t;return{variant_type:e.variantType||"variant ".concat(n+1),title:(null==ne?void 0:ne.ruleTitle)||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",is_valid:!0,is_readOnly:e.variantId!==Ln,variant_id:e.variantId,tier_id:(null===(t=e.tier)||void 0===t?void 0:t.tier_id)||"1",is_user_accepted_risk:e.is_user_accepted_risk||!1}})),i){var o=Xi||tr,l=a.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===o||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===o||(null==e?void 0:e.id)&&e.id===o});if(-1===l&&void 0!==xt&&(l=xt),-1!==l)return a[l]=e(e({},a[l]),{is_user_accepted_risk:!!r.is_user_accepted_risk}),a}return t})}},"RHS Validation Agent Response":function(t){var i,r,a,o=t;if("string"==typeof t)try{o=JSON.parse(t)}catch(e){o=C(t)}if(qe(o),console.log("--- [RHS] Updating Staged Logic ---"),console.log("parsed data from RHS Validation",o),o){Pt(function(n){var t=n;if(!t){console.log("⚠️ No staged response found! Constructing fallback from current UI state...");var i=null;if(Array.isArray(gt)){var r=Xi||tr,a=-1;!(!ne&&!ie)&&r&&(a=gt.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===r||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===r||(null==e?void 0:e.id)&&e.id===r})),-1===a&&void 0!==xt&&(a=xt),-1!==a&&(i=gt[a])}t=i?e({},i):{wta:"",wnta:""}}return e(e({},t),{wta:t.wta,wnta:t.wnta,is_valid:o.is_valid,conflictingConditions:o.conflictingConditions||[],conflictMarkdown:{},conflict_view:o.conflict_view||{}})});var l=(te||"Tier 1").toLowerCase().replace(/ /g,"_"),s=function(){var e,n=Fn.current;if(Array.isArray(n)&&n.length>0){var t,i=n.findIndex(function(e){return!0===(null==e?void 0:e.is_new_variant)});if(-1!==i)return console.log("[RHS] Resolved via is_new_variant flag:",i),i;if(Ln)if(-1!==(t=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===Ln||(null==e?void 0:e.new_variant_id)===Ln})))return t;if(Xi)if(-1!==(t=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===Xi||(null==e?void 0:e.new_variant_id)===Xi})))return t;var r=null!==(e=null==ie?void 0:ie.length)&&void 0!==e?e:0;if(n.length>r){var a=n.length-1;return console.log("[RHS] Resolved via length diff (new variant appended):",a),a}}var o=void 0!==xt?xt:0;return console.log("[RHS] Fallback index used:",o),o}();if(console.log("Resolved variant index for conflict injection:",s),!1===o.is_valid)((null===(i=o.conflictingConditions)||void 0===i?void 0:i.length)||0)>0&&(Ei(function(n){var t=e({},n);return o.conflictingConditions.forEach(function(e){e.condition_wnta&&!e.condition_wta?t["whenNotToApply-".concat(l,"-").concat(s)]=e.reason:t["whenToApply-".concat(l,"-").concat(s)]=e.reason}),t}),Li("")),o.conflict_view&&((null===(r=o.conflict_view.wta)||void 0===r?void 0:r.length)>0&&ct(function(t){var i=new Map(t),r=new Map(i.get(l)||new Map),a=n([],r.get(s)||[],!0);return o.conflict_view.wta.forEach(function(n){var t=a.findIndex(function(e){return e.text.trim()===n.value});-1!==t?a[t]=e(e({},a[t]),{conflict:n}):a.push({id:"conflict-wta-".concat(Date.now(),"-").concat(Math.random()),text:n.value,conflict:n})}),r.set(s,a),i.set(l,r),i}),(null===(a=o.conflict_view.wnta)||void 0===a?void 0:a.length)>0&&pt(function(t){var i=new Map(t),r=new Map(i.get(l)||new Map),a=n([],r.get(s)||[],!0);return o.conflict_view.wnta.forEach(function(n){var t=a.findIndex(function(e){return e.text.trim()===n.value});-1!==t?a[t]=e(e({},a[t]),{conflict:n}):a.push({id:"conflict-wnta-".concat(Date.now(),"-").concat(Math.random()),text:n.value,conflict:n})}),r.set(s,a),i.set(l,r),i}));else!0===o.is_valid&&(Ei({}),Li(""),ct(function(n){var t=new Map(n),i=new Map(t.get(l)||new Map),r=i.get(s)||[];return i.set(s,r.map(function(n){return e(e({},n),{conflict:void 0})})),t.set(l,i),t}),pt(function(n){var t=new Map(n),i=new Map(t.get(l)||new Map),r=i.get(s)||[];return i.set(s,r.map(function(n){return e(e({},n),{conflict:void 0})})),t.set(l,i),t}))}},"User Action Execution":function(e){console.log("--- [User Action] Execution ---",e);var n=E(e);(console.log("parsedaction",n),"load"===(null==n?void 0:n.action)&&(null==n?void 0:n.question_id)&&oa(n,null),"save"===(null==n?void 0:n.action)&&(null==n?void 0:n.is_save)&&(ji(!0),Gi(!0)),"discard_rule"===(null==n?void 0:n.action))&&(mt([{variant_id:void 0,rule_description:"",wta:"",wnta:"",variant_type:"variant"}]),At([]),Rn(null),Ot(0),ti(0),Nn(null),An(!1),mn(!1),qt([]),Dn.current=null,tt(""),or(null),Kn(new Map([["tier_1",new Map([[0,Bn]])],["tier_2",new Map([[0,Bn]])]])),st(new Map([["tier_1",new Map],["tier_2",new Map]])),ct(new Map([["tier_1",new Map],["tier_2",new Map]])),pt(new Map([["tier_1",new Map],["tier_2",new Map]])))},"Validation Agent Response":function(t){var i,r;if(console.log("--- [Validation Agent] Committing Staged Data to UI ---"),console.log("raw data from Validation Agent",t,typeof t),"string"==typeof t){var a=t.match(/<!--\s*```json\s+property="rule_validation_card"\s+([\s\S]*?)```\s*-->/i);if(a&&a[1])try{var o=JSON.parse(a[1].trim()),l=null===(r=null===(i=null==o?void 0:o.validated_rules)||void 0===i?void 0:i[0])||void 0===r?void 0:r.rule_id;l&&(console.log("Setting default comparison ID:",l),cn(l))}catch(e){console.error("Failed to parse rule_validation_card JSON",e)}}var s=N(t,"critical_gaps");console.log("isCriticalGap",s),Xe(!!s);var u="string"==typeof t?E(t):t;console.log("parsed data from Validation Agent",u);var d=x(t);Je(u);var c=!!(null==u?void 0:u.is_variant),v=void 0===(null==u?void 0:u.close_modal)||!!(null==u?void 0:u.close_modal),_=void 0!==(null==u?void 0:u.is_save)&&!!(null==u?void 0:u.is_save);ji(v),Gi(_);var p=null==u?void 0:u.action,f="string"==typeof(null==u?void 0:u.edit_variant_id)&&u.edit_variant_id.trim()?u.edit_variant_id:"string"==typeof(null==u?void 0:u.set_variant_id)&&u.set_variant_id.trim()?u.set_variant_id:null,g="variant"===p&&c;if((null==u?void 0:u.edit_variant_id)&&!g&&er(u.edit_variant_id),"edit"!==p&&"edit_current_rule"!==p||!f||ir(f),g&&(er(null),ir(null)),"discard"===p)return console.log("Action 'discard' received: Resetting UI state but preserving chat."),hn.current=!0,Mr(!0),mt([]),wi.current=null,void(yi.current=null);Zi(p);var m=null==u?void 0:u.new_variant_id,w="edit"===(null==u?void 0:u.action)&&(null==u?void 0:u.set_variant_id)&&!ie;(null==u?void 0:u.is_valid)?(Ei({}),Pt(function(t){var i,r,a,o,l,s,d,c,v,_,p,g;if(console.log("⏮️ PREVIOUS Staged Response:",t),!t)return console.warn("⚠️ No previous staged response found"),t;var m=function(e,n){return Object.prototype.hasOwnProperty.call(e||{},n)},w=e(e({},t),u);Array.isArray(null===(i=null==u?void 0:u.condition_result)||void 0===i?void 0:i.when_to_apply)?w.wta=u.condition_result.when_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join(" OR "):m(u,"wta")&&void 0!==(null==u?void 0:u.wta)&&(w.wta=null!==(r=null==u?void 0:u.wta)&&void 0!==r?r:""),Array.isArray(null===(a=null==u?void 0:u.condition_result)||void 0===a?void 0:a.when_not_to_apply)?w.wnta=u.condition_result.when_not_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join(" OR "):m(u,"wnta")&&void 0!==(null==u?void 0:u.wnta)&&(w.wnta=null!==(o=null==u?void 0:u.wnta)&&void 0!==o?o:""),console.log("finalData",w),console.log("checking parsed here :",u);var y,h=f;if(Array.isArray(gt)&&h&&(y=h,gt.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===y||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===y||(null==e?void 0:e.id)&&e.id===y})),"edit"!==(null==u?void 0:u.action)||!ne&&!ie)return w;console.log("inside edit flow merge");var b=ie||[],T=f,A=T?b.findIndex(function(e){return e.variantId===T||e.variant_id&&e.variant_id===T||e.id&&e.id===T}):-1;if(-1===A&&(null==u?void 0:u.set_variant_id)&&(A=b.findIndex(function(e){return e.variantId===u.set_variant_id||e.variant_id&&e.variant_id===u.set_variant_id||e.id&&e.id===u.set_variant_id})),-1===A&&T&&Array.isArray(gt)&&(A=gt.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===T||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===T||(null==e?void 0:e.id)&&e.id===T})),-1!==A){var M,I,k,S=b[A]||(null==gt?void 0:gt[A]),N=(null==S?void 0:S.tier_id)||(null==S?void 0:S.tierId)||"1",C=(null==te?void 0:te.toLowerCase().replace(/ /g,"_"))||"tier_".concat(N);if(hi.current!==A&&(Ot(A),hi.current=A),Array.isArray(null===(l=u.condition_result)||void 0===l?void 0:l.when_to_apply)?M=u.condition_result.when_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join("\n"):m(u,"wta")&&void 0!==(null==u?void 0:u.wta)?M=null!==(s=null==u?void 0:u.wta)&&void 0!==s?s:"":m(t,"wta")&&void 0!==(null==t?void 0:t.wta)&&(M=null!==(d=null==t?void 0:t.wta)&&void 0!==d?d:""),Array.isArray(null===(c=u.condition_result)||void 0===c?void 0:c.when_not_to_apply)?I=u.condition_result.when_not_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join("\n"):m(u,"wnta")&&void 0!==(null==u?void 0:u.wnta)?I=null!==(v=null==u?void 0:u.wnta)&&void 0!==v?v:"":m(t,"wnta")&&void 0!==(null==t?void 0:t.wnta)&&(I=null!==(_=null==t?void 0:t.wnta)&&void 0!==_?_:""),void 0!==M){var E=Cr(M);Er(C,A,M,E,dt,ct,null==u?void 0:u.conflict_view),br(C,A,"wta")}if(void 0!==I){var x=Cr(I);Er(C,A,I,x,_t,pt,null==u?void 0:u.conflict_view),br(C,A,"wnta")}if(m(u,"rule_description")&&void 0!==(null==u?void 0:u.rule_description)?k=null!==(p=null==u?void 0:u.rule_description)&&void 0!==p?p:"":m(t,"rule_description")&&void 0!==(null==t?void 0:t.rule_description)&&(k=null!==(g=null==t?void 0:t.rule_description)&&void 0!==g?g:""),void 0!==k){var O=Cr(k);st(function(e){var n=new Map(e),t=new Map(n.get(C)||new Map);return t.set(A,O),n.set(C,t),n}),br(C,A,"ruleDescription")}var L=void 0;m(u,"scheduling_config")&&void 0!==(null==u?void 0:u.scheduling_config)?L=null==u?void 0:u.scheduling_config:m(t,"scheduling_config")&&void 0!==(null==t?void 0:t.scheduling_config)&&(L=null==t?void 0:t.scheduling_config),L&&(Kn(function(e){var n,i,r,a,o,l,s=new Map(e),u=new Map(s.get(C)||new Map);return u.set(A,{order_rules:{enabled:!!(null===(n=L.order_rules)||void 0===n?void 0:n.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(i=L.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)},advertiser_rules:{enabled:!!(null===(r=L.advertiser_rules)||void 0===r?void 0:r.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(a=L.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)},inventory_rules:{respect_inventory_code:!!(null===(o=L.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)},break_code_rules:{respect_break_code:!!(null===(l=L.break_code_rules)||void 0===l?void 0:l.respect_break_code)},is_default_enabled:"Default"===(null==t?void 0:t.variant_type)||"default_variant"===(null==t?void 0:t.variant_type)||"default"===(null==t?void 0:t.variant_type)}),s.set(C,u),s}),br(C,A,"order_enabled"),br(C,A,"advertiser_enabled"),br(C,A,"respect_inventory_code"),br(C,A,"respect_break_code")),mt(function(t){if(!Array.isArray(t)||A<0||!t[A])return t;var i=n([],t,!0),r=e({},i[A]);return void 0!==M&&(r.wta=M),void 0!==I&&(r.wnta=I),void 0!==k&&(r.rule_description=Cr(k)),L&&(r.scheduling_config=L),console.log("updated variantxxx",r),i[A]=r,i});var R=null==t?void 0:t.variant_type;R&&mt(function(t){var i=Array.isArray(t)?n([],t,!0):[],r=i.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===T||(null==e?void 0:e.variant_id)&&e.variant_id===(null==u?void 0:u.set_variant_id)||(null==e?void 0:e.variantId)===T});return-1!==r?i[r]=e(e({},i[r]),{variant_type:R}):i[A]=e(e({},i[A]||{}),{variant_type:R,variant_id:T||(null==u?void 0:u.set_variant_id)}),i})}})):console.warn("⚠️ parsed.is_valid is false, not committing to structuredAIResponse"),w||(c?(kt(!0),m&&m!==yi.current?(console.log("🚀 New Variant detected (".concat(m,"). Triggering loader.")),Si(!0),Bi("Loading a variant"),console.log("reaching here in new variant flow"),Ki("This may take a moment as we update the rule with the new variant."),yi.current=m):m||console.log("ℹ️ Variant flow detected without specific ID.")):(kt(!1),yi.current=null),d&&qt(d))},"Rule comparison results":function(e){try{var n=e;if("string"==typeof e)try{n=JSON.parse(e)}catch(t){n=E(e)||[]}if(!Array.isArray(n)){if(!n||"object"!=typeof n)return void console.error("[Rule comparison results] Data is not an array:",e);n=[n]}var t=n.map(function(e,n){var t,i,r,a,o=null,l=null;if((Array.isArray(e)?e:[e]).forEach(function(e){e&&(e.intent_output&&(o=e.intent_output),e.behaviour_output&&(l=e.behaviour_output))}),"string"==typeof o)try{o=JSON.parse(o)}catch(e){console.error("Failed to parse intent_output",e)}if("string"==typeof l)try{l=JSON.parse(l)}catch(e){console.error("Failed to parse behaviour_output",e)}var s="existing_variant",u="fallback-id-".concat(Math.random()),d=(null==o?void 0:o.documents)||(null==l?void 0:l.documents);if(d){var c=Object.keys(d).find(function(e){return"reference"===d[e].role});c&&(s=c,u="".concat(c,"-").concat(n))}var v="";if((null==o?void 0:o.nodes)&&o.nodes.title){var _=o.nodes.title;(null===(i=null===(t=null==_?void 0:_.comparison_inputs)||void 0===t?void 0:t[s])||void 0===i?void 0:i.content)&&(v=_.comparison_inputs[s].content)}var p="Score";(null==l?void 0:l.final_score)&&(p=l.final_score);var f="Unknown Tier",g=(null===(r=null==d?void 0:d[s])||void 0===r?void 0:r.title)||(null===(a=null==d?void 0:d.existing_variant)||void 0===a?void 0:a.title);return g&&g.includes("|")&&(f=g.split("|")[1].trim()),console.log("Extracted for carousel:",{uniqueId:u,extractedTitle:v,extractedScore:p,extractedScope:f}),{id:u,title:v,similarityScoreText:p,scope:f,intentTreeData:o,behaviorTreeData:l,isLoading:!1}});sn(t)}catch(e){console.error("[Rule comparison results] Critical Handler Error:",e)}}},Nr=function(e){var n=Array.isArray(e)?e.join(" "):String(e||""),t=null==n?void 0:n.match(/<ai_reasoning>([\s\S]*?)<\/ai_reasoning>/);return t?t[1]:null},Cr=function(e){return"string"!=typeof e?"":e.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,"").trim()},Er=function(t,i,r,a,o,l,s){var u;if(void 0!==r){var d=(o.get(t)||new Map).get(i)||[],c=d.map(function(e){return e.text}).join("\n"),v=(null===(u=d[(null==d?void 0:d.length)-1])||void 0===u?void 0:u.reasoning)||"",_=Nr(r)||"";if(""===r&&d.length>0||a.trim()!==c.trim()||_!==v){var p=""===r?{updatedItems:[],newIds:[]}:function(t,i,r){if(!t.trim())return{updatedItems:n([],i,!0),newIds:[]};var a=t.match(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g)||[],o=t.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,""),l=[],s=o.replace(/<conflicting>[\s\S]*?<\/conflicting>/g,function(e){return l.push(e),"__CONFLICT_BLOCK_".concat((null==l?void 0:l.length)-1,"__")}).split(/\s*(?:\r?\n|\\+n)\s*/).map(function(e){return e.trim().replace(/^-\s*/,"")}).filter(Boolean),u=new Map(i.map(function(e){return[e.text,e]})),d=[],c=[],v=Array.isArray(null==r?void 0:r.wta)&&t.includes("wta")?r.wta:Array.isArray(null==r?void 0:r.wnta)?r.wnta:[];return s.forEach(function(n,t){var i=n;i.includes("__CONFLICT_BLOCK_")&&(i=i.replace(/__CONFLICT_BLOCK_(\d+)__/g,function(e,n){return l[parseInt(n)]}));var r=a[t]||"",o=i.replace(/<conflicting>[\s\S]*?<\/conflicting>/g,"").trim(),s=null==v?void 0:v.find(function(e){return e.value===o}),_=u.get(i);if(_)c.push(e(e({},_),{reasoning:r||_.reasoning,conflict:s||_.conflict}));else{var p="new-".concat(Date.now(),"-").concat(t);d.push(p),c.push({id:p,text:i,reasoning:r,conflict:s})}}),{updatedItems:c,newIds:d}}(r,d,s),f=p.updatedItems,g=p.newIds;l(function(e){var n=new Map(e),r=new Map(n.get(t)||new Map);return r.set(i,f),n.set(t,r),n}),(null==g?void 0:g.length)>0&&(Xt(function(e){return new Set(n(n([],Array.from(e),!0),g,!0))}),setTimeout(function(){Xt(function(e){var n=new Set(e);return g.forEach(function(e){return n.delete(e)}),n})},1500))}}},xr=function(e){return(Array.isArray(e)?e:e?[e]:[]).map(function(e){return String(e).replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/,"").trim().replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()})})};d(function(){Rt&&!De.current&&"save this learning"!==Rt.toLowerCase()&&(Ee(!1),Se(""),hi.current=null!=xt?xt:-1)},[Rt]),d(function(){var t,i,r,a,o,l,s,u,d;if(Gn.current=li,Array.isArray(gt)&&gt.length>0){var c=di||"local-".concat(Date.now()),v=["ruleTitle","ruleDescription","whenToApply","whenNotToApply","scopeData","configData"],_={},p=wi.current||[];gt.forEach(function(t,i){var r,a;if(t){t.is_readOnly||(nn.current=i);var o=i;if((null==t?void 0:t.variant_id)&&ie)-1!==(l=ie.findIndex(function(e){return e.variantId===t.variant_id}))&&(o=l);else if(Xi&&ie&&1===gt.length){var l;-1!==(l=ie.findIndex(function(e){return e.variantId===Xi}))&&(o=l)}var s=null!==(r=p[o])&&void 0!==r?r:p[i],u=null!==(a=t.scope)&&void 0!==a?a:null==s?void 0:s.scope,d=xr(u).map(function(e){return e.toLowerCase().replace(/ /g,"_")}),c=d.length>0?d:yn.current.length>0?yn.current:["tier_1"];c.length>0&&(yn.current=c),console.log("prevTierRef.current",yn.current,"targetTiers",c);var f=Cr(t.title);f&&f!==Cr(null==s?void 0:s.title)&&!t.is_readOnly&&function(e,n,t,i,r,a){var o=Cr(i);o&&o!==r&&(br(e,n,t),a(o))}("global",0,"ruleTitle",t.title,nt,tt),c.forEach(function(r){var a,l,s,u,d,c=null!==(a=p[o])&&void 0!==a?a:p[i],f=!c,g=void 0!==t.rule_description&&null!==t.rule_description,m=Cr(t.rule_description),w=(null===(l=lt.get(r))||void 0===l?void 0:l.get(o))||"",y=Cr(w);g&&(m!==y||""===m&&""!==w)&&(st(function(e){var n=new Map(e),i=new Map(n.get(r)||new Map);return i.set(o,t.rule_description),n.set(r,i),n}),br(r,o,"ruleDescription"));var h=!1===(null==Vt?void 0:Vt.is_valid),b=Cr(t.wta),T=Cr(null==c?void 0:c.wta),A=(null===(s=dt.get(r))||void 0===s?void 0:s.get(o))||[];!(h&&""===b&&A.length>0)&&void 0!==t.wta&&null!==t.wta&&(b!==T||""===b&&A.length>0||0===A.length&&""!==b)&&(Er(r,o,t.wta,b,dt,ct,null==Vt?void 0:Vt.conflict_view),br(r,o,"wta"));var M=Cr(t.wnta),I=Cr(null==c?void 0:c.wnta),k=(null===(u=_t.get(r))||void 0===u?void 0:u.get(o))||[];if(!(h&&""===M&&k.length>0)&&void 0!==t.wnta&&null!==t.wnta&&(M!==I||""===M&&k.length>0||0===k.length&&""!==M)&&(Er(r,o,t.wnta,M,_t,pt,null==Vt?void 0:Vt.conflict_view),br(r,o,"wnta")),t.scheduling_config||f||t.variant_type||t.variantType){var S=t.scheduling_config||{order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1}},N=null==c?void 0:c.scheduling_config,C=null===(d=Yn.get(r))||void 0===d?void 0:d.get(o),E=t.variant_type||t.variantType,x=E?U(E):void 0,O=JSON.stringify(S)!==JSON.stringify(N),L=void 0!==x&&x!==(null==C?void 0:C.is_default_enabled),R=function(n,t){if(!n)return t;if(!t)return n;var i=e({},n);for(var r in t)t[r]&&"object"==typeof t[r]&&!Array.isArray(t[r])?i[r]=e(e({},i[r]||{}),t[r]):i[r]=t[r];return i};if(Yt(function(e){return R(e,S)}),O||L){Yt(function(e){return R(e,S)});var D=function(e,n,t,i){var r,a,o,l,s=[],u=(i.get(e)||new Map).get(n)||{order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1}};return Math.floor(((null===(r=t.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)||0)/60)!==u.order_rules.global_min_separation_seconds&&s.push("config_order_rules"),Math.floor(((null===(a=t.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)!==u.advertiser_rules.global_min_separation_seconds&&s.push("config_advertiser_rules"),!!(null===(o=t.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)!==u.inventory_rules.respect_inventory_code&&s.push("config_inventory_rules"),!!(null===(l=t.break_code_rules)||void 0===l?void 0:l.respect_break_code)!==u.break_code_rules.respect_break_code&&s.push("config_break_code_rules"),!!t.is_default_enabled!=!!u.is_default_enabled&&s.push("config_is_default_enabled"),s}(r,o,S,Yn);D.forEach(function(e){return br(r,o,e)}),Zn(function(e){return new Set(e).add("config")}),Kn(function(n){var t,i,a,l,s,u,d=new Map(n),c=new Map(d.get(r)||new Map),v=c.get(o)||Bn;return c.set(o,e(e(e(e(e(e({},v),S.order_rules&&{order_rules:{enabled:!!(null===(t=S.order_rules)||void 0===t?void 0:t.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(i=S.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)}}),S.advertiser_rules&&{advertiser_rules:{enabled:!!(null===(a=S.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(l=S.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)||0)/60)}}),S.inventory_rules&&{inventory_rules:{respect_inventory_code:!!(null===(s=S.inventory_rules)||void 0===s?void 0:s.respect_inventory_code)}}),S.break_code_rules&&{break_code_rules:{respect_break_code:!!(null===(u=S.break_code_rules)||void 0===u?void 0:u.respect_break_code)}}),void 0!==x&&{is_default_enabled:x})),d.set(r,c),d}),at(function(e){var n=new Map(e),t=new Map(n.get(r)||new Map),i=t.get(o)||new Set;return S.order_rules&&i.add("order_rules"),S.advertiser_rules&&i.add("advertiser_rules"),S.inventory_rules&&i.add("inventory_rules"),S.break_code_rules&&i.add("break_code_rules"),void 0!==x&&i.add("is_default_enabled"),t.set(o,i),n.set(r,t),n})}}var j={ruleTitle:t.title,ruleDescription:t.rule_description,whenToApply:t.wta,whenNotToApply:t.wnta,scopeData:t.scope,configData:t.action};t.is_readOnly||v.forEach(function(e){var n=Nr(j[e]),t="ruleTitle"===e||"scopeData"===e?e:"".concat(e,"_").concat(r);n&&(n!==Pn.current[t]&&(Pn.current[t]=n,qn.current[t]=!0,_[t]={reasoning:n,associatedCitations:[],tier:r,variantIndex:o}))}),t.conflictingConditions&&(Ei(function(n){var i=e({},n);return t.conflictingConditions.forEach(function(e){e.condition_wta&&(i["whenToApply-".concat(r,"-").concat(o)]=e.reason),e.condition_wnta&&(i["whenNotToApply-".concat(r,"-").concat(o)]=e.reason)}),i}),Zn(function(e){return new Set(n(n([],Array.from(e),!0),["conditions"],!1))}))})}});var f=-1,g=!(!ne&&!ie),m="edit"===zi||"edit_current_rule"===zi,w=function(e,n){return(null==e?void 0:e.variant_id)&&e.variant_id===n||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===n||(null==e?void 0:e.id)&&e.id===n},y=function(e,n){if(!n)return-1;var t=e.find(function(e){return w(e,n)});if(!t)return-1;var i=String((null==t?void 0:t.tier_id)||"");if(!i)return e.findIndex(function(e){return w(e,n)});var r=e.filter(function(e){return String((null==e?void 0:e.tier_id)||"")===i}).findIndex(function(e){return w(e,n)});return-1!==r?r:e.findIndex(function(e){return w(e,n)})};if(g)if(yi.current&&It)-1!==(f=gt.findIndex(function(e){return(null==e?void 0:e.new_variant_id)&&e.new_variant_id===yi.current||(null==e?void 0:e.variant_id)&&e.variant_id===yi.current||(null==e?void 0:e.id)&&e.id===yi.current}))&&(yi.current=null);else if(Xi||tr){var h;(h=Xi||tr)&&(f=y(gt,h))}else if(oe&&Array.isArray(gt)&&gt.length>0){for(b=gt.length-1;b>=0;b--)if(gt[b]&&!(null===(i=gt[b])||void 0===i?void 0:i.is_readOnly)){f=b;break}}else null==re||wi.current&&0!==wi.current.length||(f=re);else if((h=Xi||tr||yi.current)&&m&&(f=y(gt,h)),!m&&-1===f&&Array.isArray(gt)&&gt.length>0){for(var b=gt.length-1;b>=0;b--)if(gt[b]&&!(null===(t=gt[b])||void 0===t?void 0:t.is_readOnly)){f=b;break}-1===f&&(f=gt.length-1)}-1!==f&&xt!==f&&(hi.current===f||Ye.current&&!m||(console.log("🎯 Setting active variant accordion to index ".concat(f," (").concat(g?"EDIT":"CREATE"," mode)")),Ot(f),hi.current=f));var T=gt[0];if(T){if(T.scope&&JSON.stringify(T.scope)!==JSON.stringify(null===(r=p[0])||void 0===r?void 0:r.scope)&&"copy"!==T.action&&(l=T.scope,s=null===(a=p[0])||void 0===a?void 0:a.scope,u=xr(l),d=s?xr(s):[],ge(function(n){return e(e({},n),{tiers:u})}),u.forEach(function(e){var n=e.trim();d.includes(e)||br("global",0,"scope_".concat(n))})),T.scope_rule_set&&T.scope_rule_set!==(null===(o=p[0])||void 0===o?void 0:o.scope_rule_set)){var A=T.scope_rule_set.toLowerCase().trim();["global","genre","custom"].includes(A)&&(ge(function(n){return e(e({},n),{station:A})}),br("global",0,"scope_station"))}T.wtl&&Xn(T.wtl)}wi.current=gt.map(function(e){return e?(n=e,"function"==typeof(t=null===globalThis||void 0===globalThis?void 0:globalThis.structuredClone)?t(n):JSON.parse(JSON.stringify(n))):e;var n,t}),Object.keys(_).length>0&&(ur({type:"SAVE_CITATIONS",requestId:c,reasoningMap:_,rawCitations:li}),function(e){var n=e.requestId,t=e.keys,i=e.newReasoningMap,r=e.getLatestReasoning,a=e.currentCitations;console.group("%c Citation Audit: ".concat(n," "),"background: #5928ed; color: #fff; font-weight: bold;");var o={};t.forEach(function(e){var n=r(e),t=!!i[e],l=t?a:(null==n?void 0:n.associatedCitations)||[];o[e]={Status:t?"NEW ✅":n?"FALLBACK 🔄":"EMPTY ❌",Source:t?"Current Msg":n?"History":"N/A","Reasoning Preview":t?i[e].reasoning:(null==n?void 0:n.reasoning)||"None","Full Citations":l}}),console.table(o),console.groupEnd()}({requestId:c,keys:Object.keys(_),newReasoningMap:_,getLatestReasoning:dr,currentCitations:li}),ai(function(n){var t=e({},n),i=!1;return Object.keys(_).forEach(function(e){var n=_[e];n.reasoning&&t[e]!==n.reasoning&&(t[e]=n.reasoning,i=!0),n.associatedCitations}),i?t:n}))}},[gt,li,di]),d(function(){var t=Object.keys(qn.current);if(0!==t.length){var i={};t.forEach(function(e){i[e]=n([],li,!0)}),qn.current={},pi(function(n){return e(e({},n),i)})}},[li]);var Or=function(e){return e.map(function(e){var n;return null===(n=e.match(/\d+/))||void 0===n||n[0],e.replace(/\s+/g,"-"),{name:"Custom",parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:(null==Jn?void 0:Jn.CUSTOM_SCOPE_GROUP_NAME)||"",description:(null==ee?void 0:ee.ruleSetDesc)||""}}})},Lr=function(e){var n={};return Object.entries(e).forEach(function(e){var t,i=e[0],r=e[1];if("custom_sep_time_for_entities"===i&&r)n.custom_sep_time_for_entities=r;else if("max_priority_override"===i&&r&&void 0!==r.override_priority_code)n.override_priority_code=r.override_priority_code;else if("advertiser_pair_break_constraint"===i&&r)n.advertiser_pair_break_constraint=r.advertiser_pair_list;else if(r&&"object"==typeof r&&(null===(t=Object.keys(r))||void 0===t?void 0:t.length)>0){var a=i.replace(/_rules$/,""),o=["global_min_separation_seconds","respect_inventory_code","respect_break_code","exemption_advertiser_list","exemption_break_code_list","exemption_priority_code_list","priority_code_list","exemption_product_code_list","condition_advertiser_list","condition_break_code_list","condition_priority_code_list","condition_product_code_list"];Object.entries(r).forEach(function(e){var t=e[0],r=e[1];if(o.includes(t)&&("product_rules"!==i||"global_min_separation_seconds"!==t)){var l="respect_inventory_code"===t||"respect_break_code"===t?t:"".concat(a,"_").concat(t);n[l]=r}})}else if(void 0!==r&&"object"!=typeof r){if("station_guid"===i||"is_default_enabled"===i)return;n[i]=r}}),{config_structure:{scheduling_config:{advertiser_exemption_advertiser_list:"array",advertiser_exemption_break_code_list:"array",advertiser_exemption_priority_code_list:"array",advertiser_exemption_product_code_list:"array",advertiser_global_min_separation_seconds:"int",break_code_exemption_advertiser_list:"array",break_code_exemption_break_code_list:"array",break_code_exemption_priority_code_list:"array",break_code_exemption_product_code_list:"array",custom_sep_time_for_entities:"object",inventory_exemption_advertiser_list:"array",inventory_exemption_break_code_list:"array",inventory_exemption_priority_code_list:"array",inventory_exemption_product_code_list:"array",order_exemption_advertiser_list:"array",order_exemption_break_code_list:"array",order_exemption_priority_code_list:"array",order_exemption_product_code_list:"array",order_global_min_separation_seconds:"int",override_priority_code:"str",product_exemption_advertiser_list:"array",product_exemption_break_code_list:"array",product_exemption_priority_code_list:"array",product_exemption_product_code_list:"array",max_priority_override_priority_code_list:"array",max_priority_override_exemption_advertiser_list:"array",max_priority_override_exemption_break_code_list:"array",max_priority_override_exemption_priority_code_list:"array",max_priority_override_exemption_product_code_list:"array",first_in_break_exemption_advertiser_list:"array",first_in_break_exemption_break_code_list:"array",first_in_break_exemption_priority_code_list:"array",first_in_break_exemption_product_code_list:"array",first_in_break_condition_advertiser_list:"array",first_in_break_condition_break_code_list:"array",first_in_break_condition_priority_code_list:"array",first_in_break_condition_product_code_list:"array",advertiser_pair_break_constraint:"array",respect_break_code:"bool",respect_inventory_code:"bool"}},config_data:{scheduling_config:n}}},Rr=function(n,t,i,r){void 0===r&&(r=0);var a=e({},t||{});return n.forEach(function(n,o){var l=n.get(r);l&&l.forEach(function(n){var l,s=t[n]||{},u=i.get(o),d=null==u?void 0:u.get(r),c=null==d?void 0:d[n];if("order_rules"===n||"advertiser_rules"===n){var v=null!==(l=null==c?void 0:c.enabled)&&void 0!==l&&l?60*((null==c?void 0:c.global_min_separation_seconds)||0):0;a[n]=e(e({},s),{global_min_separation_seconds:v})}else a[n]="inventory_rules"===n?e(e({},s),{respect_inventory_code:null==c?void 0:c.respect_inventory_code}):"break_code_rules"===n?e(e({},s),{respect_break_code:null==c?void 0:c.respect_break_code}):"is_default_enabled"===n?null!=c&&c:s})}),a},Dr=function(e,n,t){void 0===t&&(t="variantType");var i=null==e?void 0:e[n];if(!i)return{label:Ir(e),isDefault:!1};var r=i[t]||"",a=U(r);return{label:"variant ".concat(n+1),isDefault:a}},jr=function(e,n){var t=U(e);return{label:"variant ".concat(n+1),isDefault:t}},Vr=function(e,n,t){var i;void 0===t&&(t="");var r=null===(i=Yn.get(e))||void 0===i?void 0:i.get(n);return r?r.is_default_enabled?"Default":"Variant":U(t)?"Default":"Variant"},Pr=function(n,o){return void 0===o&&(o=!1),t(void 0,void 0,void 0,function(){var t,l,s,u,d,c,v,p,g,w,h,b,T,A,k,S,N;return i(this,function(i){switch(i.label){case 0:t=Or(fe.tiers),l=M.getState().backwardCompatibility,s=nn.current,u=Rr(rt,Wt,Yn,s),i.label=1;case 1:return i.trys.push([1,3,,4]),ye(F),Se(""),d=Lr(u),c=m(),v=[],p=[],g=(gt||[])[s]||{},console.log("originalItem check :",g,s),fe.tiers.forEach(function(i){var r,a,o,u,_,f,w,h,b,T,A,M,I,k,S,N=i.toLowerCase().replace(/ /g,"_");p.push(N);var C=(null===(r=lt.get(N))||void 0===r?void 0:r.get(s))||"",E=(null===(a=dt.get(N))||void 0===a?void 0:a.get(s))||[],x=(null===(o=_t.get(N))||void 0===o?void 0:o.get(s))||[];if(!1===l){var O=[e(e({},d&&{config:e({master_name:Jn.CONFIG_MASTER_NAME},d)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},tierLinkId:c,variant:Cr(C),conditions_whentoapply:E.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:x.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null===(f=null===(_=null===(u=Yn.get(N))||void 0===u?void 0:u.get(s))||void 0===_?void 0:_.is_default_enabled)||void 0===f||f,tier_id:(null===(w=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===N||e.tier_name.toLowerCase()===N.replace(/_/g,"")}))||void 0===w?void 0:w.tier_id)||"",topic_id:null,scope_id:(null==ee?void 0:ee.ruleSetId)||"",question_title:nt,variantType:Vr(N,s,g.variant_type||g.variantType),instantLearningType:Jn.INSTANT_LEARNING_TYPE,is_user_accepted_risk:!1,propagation:!1,applicationMappingId:_e,modeId:"019e1e39-2030-7775-8312-0ad525700588"})];v.push(y(O,Jn.ASSISTANT_NAME))}else{O={status:"approved",metadata:e(e({userName:null!==(h=null==lr?void 0:lr.firstname)&&void 0!==h?h:"",userEmailId:null!==(b=null==lr?void 0:lr.email)&&void 0!==b?b:"",llm:D.MODEL,isVariantFlow:!0},d&&{config:e({master_name:D.CONFIG_MASTER_NAME},d)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:c,variant:Cr(C),conditions_whentoapply:E.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:x.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:t[0]||{name:D.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:D.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null===(M=null===(A=null===(T=Yn.get(N))||void 0===T?void 0:T.get(s))||void 0===A?void 0:A.is_default_enabled)||void 0===M||M,tier:{name:i.split(" ").join("").toLowerCase(),tierGroupName:D.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(i.split(" ")[1]||"1"),tier_id:(null===(I=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===N||e.tier_name.toLowerCase()===N.replace(/_/g,"")}))||void 0===I?void 0:I.tier_id)||""},topic:{id:null,value:"".concat(fe.station||X,"RuleSet"),description:null},categories:[{id:null,value:D.CATEGORY,description:D.CATEGORY}],originalQuestion:nt,questionId:n.get(N)||null,variantType:Vr(N,s,g.variant_type||g.variantType),parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(k=null==lr?void 0:lr.email)&&void 0!==k?k:"",instantLearningType:D.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(S=null==g?void 0:g.is_user_accepted_risk)&&void 0!==S?S:null==Vt?void 0:Vt.is_user_accepted_risk)},isAgentsFeedback:!0})};v.push(y(O,Jn.ASSISTANT_NAME))}}),[4,Promise.all(v)];case 2:return w=i.sent(),Re(!1),console.log("✅ All feedback published:",w),ye(P),Te(!0),h=new Map,w.forEach(function(e,n){var t,i,r=p[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;if(a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data),a&&"string"==typeof a){var o=h.get(r)||[];o.push({id:a,action:"new"}),h.set(r,o)}}),M.getState().setCreatedVariantInfo(Array.from(h.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),null===(k=bi.current)||void 0===k||k.show({severity:"success",sticky:!1,life:6e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Learning saved successfully!"}))]}))}),xe.current=setTimeout(function(){Di||o?(console.log("Closing modal after save success",{isCloseModal:Di,forceClose:o}),ra()):Mr(!0)},3e3),[3,4];case 3:return b=i.sent(),console.error("❌ Error publishing feedback:",b),Re(!1),ye(G),Ee(!0),T=(null===(S=null==b?void 0:b.response)||void 0===S?void 0:S.data)||{},console.log("errorData: ",T),(A="Variant already exists"===(null==T?void 0:T.message))||409===(null==b?void 0:b.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==T?void 0:T.message)||(null==T?void 0:T.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(N=bi.current)||void 0===N||N.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(_,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:A||409===(null==b?void 0:b.status)?"Similar rule already exists. Please update and try again.":(null==T?void 0:T.message)||(null==T?void 0:T.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,4];case 4:return[2]}})})},Gr=function(){return t(void 0,void 0,void 0,function(){var t,o,l,s,u,d,c,v,p,g,w,b,T,A,k,S,N,C,E,x,O,L,R,D,j,V;return i(this,function(i){switch(i.label){case 0:t=M.getState().tiers,o=Rr(rt,Wt,Yn),l=Lr(o),s=nn.current,u=(gt||[])[s]||{},d=(null==Vt?void 0:Vt.variant_type)||u.variant_type||"",i.label=1;case 1:for(i.trys.push([1,3,,4]),ye(F),Se(""),v=(c=ie||[]).length>0?null===(O=c.find(function(e){return(null==e?void 0:e.link_id)&&""!==e.link_id.trim()}))||void 0===O?void 0:O.link_id:null,p=(null===(L=fe.tiers[0])||void 0===L?void 0:L.toLowerCase().replace(/ /g,"_"))||"tier_1",g=(null===(R=lt.get(p))||void 0===R?void 0:R.size)||c.length,w=n([],c,!0),b=c.length;b<g;b++)w.push({isNewVariant:!0,variantId:m()});return T=[],A=[],k=Or(fe.tiers),fe.tiers.forEach(function(n){var i=n.toLowerCase().replace(/ /g,"_"),r=t.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===i||e.tier_name.toLowerCase()===i.replace(/_/g,"")}),a=lt.get(i),o=dt.get(i),s=_t.get(i);w.forEach(function(t,c){var _,p,f,g,w,b,M,I,S,N,C,E,x,O,L,R,D,j,V,P,G,F,J,H,B,W,Y,K,Q=(null==a?void 0:a.get(c))||t.description||"",z=(null==o?void 0:o.get(c))||[],Z=(null==s?void 0:s.get(c))||[];if(t.isNewVariant){A.push({tierKey:i,payloadType:"new"});var $=Rr(rt,Wt,Yn,c),X=Lr($);console.log("variant in save edit add part :",t,u);var te={status:"approved",metadata:e(e({userName:null!==(_=null==lr?void 0:lr.firstname)&&void 0!==_?_:"",userEmailId:null!==(p=null==lr?void 0:lr.email)&&void 0!==p?p:"",llm:Jn.MODEL,isVariantFlow:!0},X&&{config:e({master_name:Jn.CONFIG_MASTER_NAME},X)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:v||m(),variant:Cr(Q),conditions_whentoapply:z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:Z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:k[0]||{name:Jn.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:Jn.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null!==(w=null===(g=null===(f=Yn.get(i))||void 0===f?void 0:f.get(c))||void 0===g?void 0:g.is_default_enabled)&&void 0!==w?w:U(t.variantType||t.variant_type||(0===c?"Default":"variant")),tier:{name:n.split(" ").join("").toLowerCase(),tierGroupName:Jn.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(n.split(" ")[1]||"1")},topic:{id:null,value:"".concat(fe.station,"RuleSet"),description:null},categories:[{id:null,value:Jn.CATEGORY,description:Jn.CATEGORY}],originalQuestion:nt,questionId:null==ne?void 0:ne.ruleId,variantType:Vr(i,c,u.variant_type||""),parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(b=null==lr?void 0:lr.email)&&void 0!==b?b:"",instantLearningType:Jn.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(I=null===(M=(gt||[])[c])||void 0===M?void 0:M.is_user_accepted_risk)&&void 0!==I?I:null==Vt?void 0:Vt.is_user_accepted_risk)},isAgentsFeedback:!0})};console.log("payload (publish new variant)",te),T.push(y(te,Jn.ASSISTANT_NAME))}else{A.push({tierKey:i,payloadType:"existing",existingId:t.variantId});var ie=t.variantId===Xi,re=Vr(i,c,ie?d||t.variantType||"":t.variantType||"");console.log("isThisVariantBeingEdited variantTypeToUse :",re,ie,t,Xi);var ae={variantId:t.variantId,ruleDesc:(null==ee?void 0:ee.ruleSetDesc)||"",whenToApply:z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),whenNotToApply:Z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null!==(C=null===(N=null===(S=Yn.get(i))||void 0===S?void 0:S.get(c))||void 0===N?void 0:N.is_default_enabled)&&void 0!==C?C:U(t.variantType||t.variant_type||(0===c?"Default":"variant")),tier:{tier_id:(null==r?void 0:r.tier_id)||"",tier_name:(null==r?void 0:r.tier_name)||n,tier_priority:null!==(E=null==r?void 0:r.tier_priority)&&void 0!==E?E:null},scope_id:(null==ee?void 0:ee.ruleSetId)||"",description:Cr(Q)||"",topic:"".concat(fe.station,"RuleSet"),configuration:(null==l?void 0:l.config_data)||{},tierLinkId:null!=v?v:null,variantType:re,is_user_accepted_risk:!!(null!==(O=null===(x=(gt||[])[c])||void 0===x?void 0:x.is_user_accepted_risk)&&void 0!==O?O:null==t?void 0:t.is_user_accepted_risk),is_enabled:t.is_enabled},oe=(yt.get(i)||[]).find(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===t.variantId}),le=!0;if(oe){var se=function(e){var n=(e||"").trim();return(n=q(n)).replace(/\s+/g," ").replace(/(\s*OR\s*)/g," OR ")},ue=se(nt)===se(oe.title),de=se(Q)===se(oe.rule_description),ce=se(ae.whenToApply)===se(oe.wta),ve=se(ae.whenNotToApply)===se(oe.wnta),_e=!!ae.isDefault===U(oe.variant_type),pe=oe.scheduling_config||{},ge=null!==(D=null!==(L=pe.order_global_min_separation_seconds)&&void 0!==L?L:null===(R=pe.order_rules)||void 0===R?void 0:R.global_min_separation_seconds)&&void 0!==D?D:0,me=null!==(P=null!==(j=pe.advertiser_global_min_separation_seconds)&&void 0!==j?j:null===(V=pe.advertiser_rules)||void 0===V?void 0:V.global_min_separation_seconds)&&void 0!==P?P:0,we=null!==(J=null!==(F=null===(G=pe.inventory_rules)||void 0===G?void 0:G.respect_inventory_code)&&void 0!==F?F:pe.respect_inventory_code)&&void 0!==J&&J,ye=null!==(W=null!==(B=null===(H=pe.break_code_rules)||void 0===H?void 0:H.respect_break_code)&&void 0!==B?B:pe.respect_break_code)&&void 0!==W&&W,he=(null===(Y=Yn.get(i))||void 0===Y?void 0:Y.get(c))||Bn,be=he.order_rules.enabled===ge>0&&he.order_rules.global_min_separation_seconds===Math.floor(ge/60)&&he.advertiser_rules.enabled===me>0&&he.advertiser_rules.global_min_separation_seconds===Math.floor(me/60)&&he.inventory_rules.respect_inventory_code===we&&he.break_code_rules.respect_break_code===ye;ue&&de&&ce&&ve&&_e&&be&&(le=!1)}le?(console.log("payloadv (update existing variant)",ae),T.push(h(ae,null!==(K=null==lr?void 0:lr.email)&&void 0!==K?K:""))):(console.log("[saveEditedVariant] No changes for variant ".concat(t.variantId,", skipping API call")),T.push(Promise.resolve({data:{id:t.variantId},skipped:!0})))}})}),[4,Promise.all(T)];case 2:return S=i.sent(),console.log("✅ All variants updated:",S),ye(P),Te(!0),Re(!1),N=new Map,S.forEach(function(e,n){var t,i;if(!(null==e?void 0:e.skipped)){var r=A[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id);a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data);var o="existing"===r.payloadType?r.existingId:a;if(o&&"string"==typeof o){var l=N.get(r.tierKey)||[];l.push({id:o,action:r.payloadType}),N.set(r.tierKey,l)}}}),M.getState().setCreatedVariantInfo(Array.from(N.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),null===(D=bi.current)||void 0===D||D.show({severity:"success",sticky:!1,life:3e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Changes saved successfully!"}))]}))}),xe.current=setTimeout(function(){ra()},3e3),[3,4];case 3:return C=i.sent(),console.error("❌ Error updating variant:",C),ye(G),Ee(!0),Re(!1),E=(null===(j=null==C?void 0:C.response)||void 0===j?void 0:j.data)||{},console.log("errorData: ",E),(x="Variant already exists"===(null==E?void 0:E.message))||409===(null==C?void 0:C.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==E?void 0:E.message)||(null==E?void 0:E.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(V=bi.current)||void 0===V||V.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(_,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:x||409===(null==C?void 0:C.status)?"Similar rule already exists. Please update and try again.":(null==E?void 0:E.message)||(null==E?void 0:E.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,4];case 4:return[2]}})})},Fr=function(){return t(void 0,void 0,void 0,function(){var n,t,o,l,s,u,d,c,v,p,g,w,b,T,A,k,S,N,C,E,x,O,L,R,D,j,V;return i(this,function(i){switch(i.label){case 0:n=M.getState().tiers,t=Rr(rt,Wt,Yn),o=Lr(t),i.label=1;case 1:return i.trys.push([1,9,,10]),ye(F),Se(""),0===(l=Array.isArray(gt)?gt:[]).length?(console.warn("No variants found to save."),ye(G),[2]):(s=l.filter(function(e){return e.is_new_variant}),u=l.filter(function(e){return!e.is_new_variant&&(null==e?void 0:e.variant_id)}),console.log("[saveLoadAndEditVariant] New: ".concat(s.length,", Existing: ").concat(u.length)),d=u.length>0?null===(R=u.find(function(e){return(null==e?void 0:e.link_id)&&""!==e.link_id.trim()}))||void 0===R?void 0:R.link_id:null,c=[],v=[],p=[],g=[],w=Or(fe.tiers),fe.tiers.forEach(function(t){var i=t.toLowerCase().replace(/ /g,"_"),r=n.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===i||e.tier_name.toLowerCase()===i.replace(/_/g,"")}),a=lt.get(i),s=dt.get(i),u=_t.get(i);l.forEach(function(n,l){var _,f,y,b,T,A,M,I,k,S,N,C,E,x,O,L,R,D,j,V,P,G,F,J,H,B,W,Y=(null==a?void 0:a.get(l))||n.rule_description||n.description||"",K=(null==s?void 0:s.get(l))||[],Q=(null==u?void 0:u.get(l))||[];if(n.is_new_variant){var z=Rr(rt,Wt,Yn,l),Z=Lr(z),$=Vr(i,l,n.variant_type||n.variantType||""),ne={status:"approved",metadata:e(e({userName:null!==(_=null==lr?void 0:lr.firstname)&&void 0!==_?_:"",userEmailId:null!==(f=null==lr?void 0:lr.email)&&void 0!==f?f:"",llm:Jn.MODEL,isVariantFlow:!0},Z&&{config:e({master_name:Jn.CONFIG_MASTER_NAME},Z)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:d||m(),variant:Cr(Y),conditions_whentoapply:K.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:Q.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:w[0]||{name:Jn.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:Jn.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null!==(T=null===(b=null===(y=Yn.get(i))||void 0===y?void 0:y.get(l))||void 0===b?void 0:b.is_default_enabled)&&void 0!==T?T:U(n.variantType||n.variant_type||(0===l?"Default":"variant")),tier:{name:t.split(" ").join("").toLowerCase(),tierGroupName:Jn.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(t.split(" ")[1]||"1"),tier_id:(null==r?void 0:r.tier_id)||""},topic:{id:null,value:"".concat(fe.station||X,"RuleSet"),description:null},categories:[{id:null,value:Jn.CATEGORY,description:Jn.CATEGORY}],originalQuestion:nt,questionId:Dn.current||null,variantType:$,parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(A=null==lr?void 0:lr.email)&&void 0!==A?A:"",instantLearningType:Jn.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(M=null==n?void 0:n.is_user_accepted_risk)&&void 0!==M?M:null==Vt?void 0:Vt.is_user_accepted_risk)},isAgentsFeedback:!0})};console.log("[saveLoadAndEditVariant] publish payload (new variant)",ne),v.push({payload:ne,tierKey:i}),g.push({tierKey:i})}else if(null==n?void 0:n.variant_id){p.push({tierKey:i,existingId:n.variant_id});var te=(null==n?void 0:n.variant_id)&&n.variant_id===ar?(null==Vt?void 0:Vt.variant_type)||n.variant_type||n.variantType||"":n.variant_type||n.variantType||"",ie=($=Vr(i,l,te),{variantId:n.variant_id,ruleDesc:(null==ee?void 0:ee.ruleSetDesc)||"",whenToApply:K.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),whenNotToApply:Q.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null!==(S=null===(k=null===(I=Yn.get(i))||void 0===I?void 0:I.get(l))||void 0===k?void 0:k.is_default_enabled)&&void 0!==S?S:U(n.variantType||n.variant_type||(0===l?"Default":"variant")),tier:{tier_id:(null==r?void 0:r.tier_id)||"",tier_name:(null==r?void 0:r.tier_name)||t,tier_priority:null!==(N=null==r?void 0:r.tier_priority)&&void 0!==N?N:null},scope_id:(null==ee?void 0:ee.ruleSetId)||"",description:Cr(Y)||"",topic:"".concat(fe.station,"RuleSet"),configuration:(null==o?void 0:o.config_data)||{},tierLinkId:null!=d?d:null,variantType:$,is_user_accepted_risk:!!(null!==(C=null==n?void 0:n.is_user_accepted_risk)&&void 0!==C?C:null===(E=(gt||[])[l])||void 0===E?void 0:E.is_user_accepted_risk),is_enabled:n.is_enabled}),re=(yt.get(i)||[]).find(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===(null==n?void 0:n.variant_id)}),ae=!0;if(re){var oe=function(e){var n=(e||"").trim();return(n=q(n)).replace(/\s+/g," ").replace(/(\s*OR\s*)/g," OR ")},le=oe(nt)===oe(re.title),se=oe(Y)===oe(re.rule_description),ue=oe(ie.whenToApply)===oe(re.wta),de=oe(ie.whenNotToApply)===oe(re.wnta),ce=!!ie.isDefault===U(re.variant_type),ve=re.scheduling_config||{},_e=null!==(L=null!==(x=ve.order_global_min_separation_seconds)&&void 0!==x?x:null===(O=ve.order_rules)||void 0===O?void 0:O.global_min_separation_seconds)&&void 0!==L?L:0,pe=null!==(j=null!==(R=ve.advertiser_global_min_separation_seconds)&&void 0!==R?R:null===(D=ve.advertiser_rules)||void 0===D?void 0:D.global_min_separation_seconds)&&void 0!==j?j:0,ge=null!==(G=null!==(P=null===(V=ve.inventory_rules)||void 0===V?void 0:V.respect_inventory_code)&&void 0!==P?P:ve.respect_inventory_code)&&void 0!==G&&G,me=null!==(H=null!==(J=null===(F=ve.break_code_rules)||void 0===F?void 0:F.respect_break_code)&&void 0!==J?J:ve.respect_break_code)&&void 0!==H&&H,we=(null===(B=Yn.get(i))||void 0===B?void 0:B.get(l))||Bn,ye=we.order_rules.enabled===_e>0&&we.order_rules.global_min_separation_seconds===Math.floor(_e/60)&&we.advertiser_rules.enabled===pe>0&&we.advertiser_rules.global_min_separation_seconds===Math.floor(pe/60)&&we.inventory_rules.respect_inventory_code===ge&&we.break_code_rules.respect_break_code===me;le&&se&&ue&&de&&ce&&ye&&(ae=!1)}ae?(console.log("[saveLoadAndEditVariant] update payload (existing variant)",ie),c.push(h(ie,null!==(W=null==lr?void 0:lr.email)&&void 0!==W?W:""))):(console.log("[saveLoadAndEditVariant] No changes for variant ".concat(n.variant_id,", skipping API call")),c.push(Promise.resolve({data:{id:n.variant_id},skipped:!0})))}})}),[4,Promise.all(c)]);case 2:b=i.sent(),console.log("✅ All existing variants updated:",b),T=[],A=0,k=v,i.label=3;case 3:if(!(A<k.length))return[3,8];S=k[A],i.label=4;case 4:return i.trys.push([4,6,,7]),[4,y(S.payload,Jn.ASSISTANT_NAME)];case 5:return N=i.sent(),T.push(N),[3,7];case 6:throw C=i.sent(),console.error("❌ Error publishing new variant:",C),C;case 7:return A++,[3,3];case 8:return console.log("✅ All new variants published:",T),E=new Map,b.forEach(function(e,n){var t,i;if(!(null==e?void 0:e.skipped)){var r=p[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data);var o=r.existingId||a;if(o&&"string"==typeof o){var l=E.get(r.tierKey)||[];l.push({id:o,action:"existing"}),E.set(r.tierKey,l)}}}),T.forEach(function(e,n){var t,i,r=g[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;if(a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data),a&&"string"==typeof a){var o=E.get(r.tierKey)||[];o.push({id:a,action:"new"}),E.set(r.tierKey,o)}}),M.getState().setCreatedVariantInfo(Array.from(E.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),ye(P),Te(!0),Re(!1),Ye.current=!1,null===(D=bi.current)||void 0===D||D.show({severity:"success",sticky:!1,life:3e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Changes saved successfully!"}))]}))}),xe.current=setTimeout(function(){ra()},3e3),[3,10];case 9:return x=i.sent(),console.error("❌ Error updating variant:",x),ye(G),Ee(!0),Re(!1),O=(null===(j=null==x?void 0:x.response)||void 0===j?void 0:j.data)||{},console.log("errorData: ",O),(L="Variant already exists"===(null==O?void 0:O.message))||409===(null==x?void 0:x.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==O?void 0:O.message)||(null==O?void 0:O.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(V=bi.current)||void 0===V||V.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(_,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:L||409===(null==x?void 0:x.status)?"Similar rule already exists. Please update and try again.":(null==O?void 0:O.message)||(null==O?void 0:O.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,10];case 10:return[2]}})})},qr=function(e){return"string"!=typeof e?e:e.replace(/<conflicting_text>[\s\S]*?<\/conflicting_text>/g,"").replace(/<decision_strength>[\s\S]*?<\/decision_strength>/g,"").replace(/<conflicting_reasoning>[\s\S]*?<\/conflicting_reasoning>/g,"").replace(/<conflicting_gap>[\s\S]*?<\/conflicting_gap>/g,"").replace(/<\/?conflicting>/g,"").trim()};d(function(){Pi&&!gi&&(console.log("isSaveRule received from agent — calling publish APIs directly."),Gi(!1),ne||ie?Gr():!0===Ye.current?Fr():t(void 0,void 0,void 0,function(){var e,n,t,r,a,o,l,s;return i(this,function(u){switch(u.label){case 0:u.trys.push([0,5,,6]),ye(F),e=new Map,n=function(n){var t,r,a,o,l,u;return i(this,function(i){switch(i.label){case 0:if(t=n.toLowerCase().replace(/ /g,"_"),r=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===t||e.tier_name.toLowerCase()===t.replace(/_/g,"")}),!(a=null==r?void 0:r.tier_id))return e.set(t,null),[2,"continue"];o={question:nt,orgName:Jn.ORGANIZATION_NAME,scope_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a},i.label=1;case 1:return i.trys.push([1,3,,4]),[4,w(o)];case 2:return l=i.sent(),e.set(t,(null===(s=null==l?void 0:l.data)||void 0===s?void 0:s.id)||null),console.log("✅ Question saved for ".concat(n,":"),l),[3,4];case 3:return u=i.sent(),console.error("❌ Error saving question for ".concat(n,":"),u),e.set(t,null),[3,4];case 4:return[2]}})},t=0,r=fe.tiers,u.label=1;case 1:return t<r.length?(a=r[t],[5,n(a)]):[3,4];case 2:u.sent(),u.label=3;case 3:return t++,[3,1];case 4:return Pr(e),[3,6];case 5:return o=u.sent(),l=new Map,fe.tiers.forEach(function(e){l.set(e.toLowerCase().replace(/ /g,"_"),null)}),Pr(l),console.error("❌ Error saving Instant Learning Question:",o),[3,6];case 6:return[2]}})}))},[Pi,gi]);var Ur=(null===(B=(null===(H=null==fe?void 0:fe.tiers)||void 0===H?void 0:H[0])||te||"Tier 1")||void 0===B?void 0:B.toLowerCase().replace(/ /g,"_"))||"tier_1",Jr=null!==(W=null!=xt?xt:re)&&void 0!==W?W:0,Hr=null!=xt,Br=Hr?null==gt?void 0:gt[Jr]:null,Wr=gt&&(null==gt?void 0:gt.find(function(e){return e&&!e.is_readOnly&&((null==e?void 0:e.new_variant_id)&&Jt.includes(e.new_variant_id)||(null==e?void 0:e.variant_id)&&Ft.includes(e.variant_id))})),Yr=(null==Br?void 0:Br.new_variant_id)&&Jt.includes(Br.new_variant_id)?Br.new_variant_id:(null==Wr?void 0:Wr.new_variant_id)&&Jt.includes(Wr.new_variant_id)?Wr.new_variant_id:"",Kr=!(!ne&&!ie),Qr=function(n){var t={},i=fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),r=(null==fe?void 0:fe.station)+"RuleSet",a=i.map(function(e){var n=je.find(function(n){return n.tier_name.toLowerCase().replace(/ /g,"_")===e||n.tier_name.toLowerCase()===e.replace(/_/g,"")});return(null==n?void 0:n.tier_id)||""}).filter(Boolean);return n.forEach(function(n,o){var l,s,u,d,c,v,_=(null==n?void 0:n.variant_id)&&n.variant_id||Ft[o]||"new_variant_".concat(o),p=(null===(l=n.scope)||void 0===l?void 0:l[0])||Ur,f=lt.get(p),g=dt.get(p),m=_t.get(p),w=(null==f?void 0:f.get(o))||n.rule_description||"",y=(null==g?void 0:g.get(o))||[],h=y.length>0?y.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n "):n.wta||"",b=(null==m?void 0:m.get(o))||[],T=b.length>0?b.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n "):n.wnta||"",A=Yn.get(p),M=null==A?void 0:A.get(o),I={};M&&Object.keys(M).length>0?I=e(e({},M),{order_rules:e(e({},M.order_rules),{global_min_separation_seconds:60*((null===(s=M.order_rules)||void 0===s?void 0:s.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},M.advertiser_rules),{global_min_separation_seconds:60*((null===(u=M.advertiser_rules)||void 0===u?void 0:u.global_min_separation_seconds)||0)})}):n.scheduling_config&&(I=n.scheduling_config),t[_]={rule_title:nt,variant_description:w,wta:h,ruleset_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a,wnta:T,scope:i,scope_rule_set:r,scheduling_config:I,is_user_accepted_risk:!(null===(v=null!==(d=null==n?void 0:n.is_user_accepted_risk)&&void 0!==d?d:null===(c=(gt||[])[o])||void 0===c?void 0:c.is_user_accepted_risk)||void 0===v||!v)}}),t};Tn&&console.log("customMetaData.ui_state for addvariant flow",Qr(Tt));var zr=M(function(e){return e.selectedRuleSetId});He.current={send_metadata_to_orchestrator:!1,default:["scope"],app_mapping_id:_e,save_api_payload:function(){var n;if(!(null===(n=fe.tiers)||void 0===n?void 0:n.length)||!(null==je?void 0:je.length))return[];var t=M.getState().backwardCompatibility,i=nn.current,r=(gt||[])[i]||{},a=Rr(rt,Wt,Yn,i),o=Lr(a),l=m(),s=Or(fe.tiers);return fe.tiers.map(function(n){var a,u,d,c,v,_,p,f,g,w,y,h,b,T,A,M=n.toLowerCase().replace(/ /g,"_"),I=(null===(a=lt.get(M))||void 0===a?void 0:a.get(i))||"",k=(null===(u=dt.get(M))||void 0===u?void 0:u.get(i))||[],S=(null===(d=_t.get(M))||void 0===d?void 0:d.get(i))||[];return!1===t?e(e({},o&&{config:e({master_name:Jn.CONFIG_MASTER_NAME},o)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},tierLinkId:l,variant:Cr(I),conditions_whentoapply:k.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:S.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null===(_=null===(v=null===(c=Yn.get(M))||void 0===c?void 0:c.get(i))||void 0===v?void 0:v.is_default_enabled)||void 0===_||_,tier_id:(null===(p=je.find(function(e){return(null==e?void 0:e.tier_name)&&(e.tier_name.toLowerCase().replace(/ /g,"_")===M||e.tier_name.toLowerCase()===M.replace(/_/g,""))}))||void 0===p?void 0:p.tier_id)||"",topic_id:null,scope_id:(null==ee?void 0:ee.ruleSetId)||"",question_title:nt,variantType:Vr(M,i,r.variant_type||r.variantType),instantLearningType:Jn.INSTANT_LEARNING_TYPE,is_user_accepted_risk:!1,propagation:!1,applicationMappingId:_e,modeId:"019e1e39-2030-7775-8312-0ad525700588"}):{status:"approved",metadata:e(e({userName:null!==(f=null==lr?void 0:lr.firstname)&&void 0!==f?f:"",userEmailId:null!==(g=null==lr?void 0:lr.email)&&void 0!==g?g:"",llm:D.MODEL,isVariantFlow:!0},o&&{config:e({master_name:D.CONFIG_MASTER_NAME},o)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:l,variant:Cr(I),conditions_whentoapply:k.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:S.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:s[0]||{name:D.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:D.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null===(h=null===(y=null===(w=Yn.get(M))||void 0===w?void 0:w.get(i))||void 0===y?void 0:y.is_default_enabled)||void 0===h||h,tier_id:(null===(b=je.find(function(e){return(null==e?void 0:e.tier_name)&&(e.tier_name.toLowerCase().replace(/ /g,"_")===M||e.tier_name.toLowerCase()===M.replace(/_/g,""))}))||void 0===b?void 0:b.tier_id)||"",topic:{id:null,value:"".concat(fe.station||X,"RuleSet"),description:null},categories:[{id:null,value:D.CATEGORY,description:D.CATEGORY}],originalQuestion:nt,questionId:null,variantType:Vr(M,i,r.variant_type||r.variantType),parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(T=null==lr?void 0:lr.email)&&void 0!==T?T:"",instantLearningType:D.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(A=null==r?void 0:r.is_user_accepted_risk)&&void 0!==A?A:null==Vt?void 0:Vt.is_user_accepted_risk)},isAgentsFeedback:!0})}})}(),ui_state:Kr?{variants:function(){var n={},t=Yn.get(Ur);if(!t)return n;var i=lt.get(Ur),r=dt.get(Ur),a=_t.get(Ur),o=fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),l=(null==fe?void 0:fe.station)+"RuleSet",s=o.map(function(e){var n=je.find(function(n){return n.tier_name.toLowerCase().replace(/ /g,"_")===e||n.tier_name.toLowerCase()===e.replace(/_/g,"")});return(null==n?void 0:n.tier_id)||""}).filter(Boolean);return t.forEach(function(u,d){var c,v,_,p,f,g,m=null===(c=null==ie?void 0:ie[d])||void 0===c?void 0:c.variantId,w=(gt||[])[d],y=(null==w?void 0:w.new_variant_id)||"",h=m||y||Ft[d]||"variant_".concat(d),b=(null==i?void 0:i.get(d))||"",T=((null==r?void 0:r.get(d))||[]).map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n ")||"",A=((null==a?void 0:a.get(d))||[]).map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n ")||"",M=t.get(d),I={};M&&Object.keys(M).length>0&&(I=e(e({},M),{order_rules:e(e({},M.order_rules),{global_min_separation_seconds:60*((null===(v=M.order_rules)||void 0===v?void 0:v.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},M.advertiser_rules),{global_min_separation_seconds:60*((null===(_=M.advertiser_rules)||void 0===_?void 0:_.global_min_separation_seconds)||0)})})),n[h]={rule_title:nt,variant_description:b,wta:T,ruleset_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:s,wnta:A,scope:o,scope_rule_set:l,scheduling_config:I,is_user_accepted_risk:!(null===(g=null!==(p=null==w?void 0:w.is_user_accepted_risk)&&void 0!==p?p:null===(f=null==ie?void 0:ie[d])||void 0===f?void 0:f.is_user_accepted_risk)||void 0===g||!g)}}),n}()}:function(){var n,t,i,r,a,o,l=(gt||[]).findIndex(function(e){return!0===e.is_new_variant}),s=-1;return-1!==l?s=l:gn?s=-1:Tn||(s=Jr),{new_current_variant:-1!==s?{rule_title:nt,rule_description:(null===(n=lt.get(Ur))||void 0===n?void 0:n.get(s))||"",wta:(null===(i=null===(t=dt.get(Ur))||void 0===t?void 0:t.get(s))||void 0===i?void 0:i.map(function(e){return qr(e.text)}).filter(function(e){return""!==e}).join(" \\n "))||"",wnta:(null===(a=null===(r=_t.get(Ur))||void 0===r?void 0:r.get(s))||void 0===a?void 0:a.map(function(e){return qr(e.text)}).filter(function(e){return""!==e}).join(" \\n "))||"",scope:fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),scope_rule_set:(null==fe?void 0:fe.station)+"RuleSet",scheduling_config:function(){var n,t,i,r=null===(n=Yn.get(Ur))||void 0===n?void 0:n.get(s);return r&&0!==Object.keys(r).length?e(e({},r),{order_rules:e(e({},r.order_rules),{global_min_separation_seconds:60*((null===(t=r.order_rules)||void 0===t?void 0:t.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},r.advertiser_rules),{global_min_separation_seconds:60*((null===(i=r.advertiser_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)})}):{}}(),is_user_accepted_risk:!!(null===(o=(gt||[])[s])||void 0===o?void 0:o.is_user_accepted_risk)}:{rule_title:"",rule_description:"",wta:"",wnta:"",scope:[],scope_rule_set:"",scheduling_config:{},is_user_accepted_risk:!1},variants:Tn?Qr(Tt):{}}}(),rule_id_in_current_session:Nt,new_variant_id:function(){var e;return Hr&&ne?(null===(e=null==ie?void 0:ie[Jr])||void 0===e?void 0:e.variantId)?null:Yr:null}(),edit_variant_id:Ln,rule_id:Dn.current,backward_compatibility:M.getState().backwardCompatibility,ruleset_id:zr,current_variants_length:ne?Array.isArray(gt)&&gt.length>0?Math.max((null===(Y=null==ne?void 0:ne.variants)||void 0===Y?void 0:Y.length)||0,gt.length):null===(K=null==ne?void 0:ne.variants)||void 0===K?void 0:K.length:void 0,tiers:M.getState().tiers,authToken:(null==lr?void 0:lr.accessToken)||""};var Zr=Ir(Tt),$r={ruleTitle:nt,scopeData:fe,variantsByTier:Array.from(Yn.keys()).reduce(function(n,t){var i=Yn.get(t)||new Map,r=lt.get(t)||new Map,a=dt.get(t)||new Map,o=_t.get(t)||new Map;return console.log("tier config map",i),n[t]=Array.from(i.keys()).map(function(n,l){var s,u,d=(gt||[])[n]||{};console.log("Original item for tier",t,"index",n,":",d);var c={label:"",isDefault:!1};if(d.is_variant&&!U(d.variant_type))c={label:"",isDefault:!1};else if(ie&&ie.length>0){c=Dr(ie,n,"variantType"),console.log("Found matching variant at index",n,":",c);var v=ie[n];if(v){if(!c.label){var _=v.variantType||"",p=U(_);c={label:"variant ".concat(n+1),isDefault:p}}if(v.variantId===Xi)(g=d.variant_type||(null==Vt?void 0:Vt.variant_type))&&(c=jr(g,n),console.log("[templateState] editingVariants[".concat(n,']: using CARE variant_type "').concat(g,'" → label "').concat(c.label,'"')));console.log("Computed variant type at index",n,":",c)}else c={label:Ir(ie),isDefault:!1},console.log("No match found, using getNextVariantType:",c)}else if(Tt&&Tt.length>0){if(c=Dr(Tt,n,"variant_type"),console.log("Found matching variant at index",n,":",c),Ye.current){var f=Tt[n];if((null==f?void 0:f.variant_id)===ar||(null==f?void 0:f.variantId)===ar)(g=d.variant_type||(null==Vt?void 0:Vt.variant_type))&&(c=jr(g,n),console.log("[templateState] existingVariants[".concat(n,']: load-and-edit using CARE variant_type "').concat(g,'" → label "').concat(c.label,'"')))}}else{var g=d.variant_type||"";c={label:Zr,isDefault:U(g)}}console.log("Computed variant data for tier",t,"index",n,":",c);var m=null!==(u=null===(s=i.get(n))||void 0===s?void 0:s.is_default_enabled)&&void 0!==u?u:c.isDefault;console.log("live is default :",m);var w="variant ".concat(l+1),y=r.get(n)||"";return{variantId:Ft[n]||d.variant_id||d.variantId,variantType:w,isDefault:m,isModified:d.is_modified,tier:t,index:n,ruleDescription:y.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,""),whenToApplyConditions:(a.get(n)||[]).flatMap(function(n){var t,i=(null===(t=null==n?void 0:n.text)||void 0===t?void 0:t.split(/\s+OR\s+/))||[];return i.length>1?i.map(function(t,i){return e(e({},n),{id:"split-".concat(n.id,"-").concat(i),text:t.trim()})}):[n]}),whenNotToApplyConditions:(o.get(n)||[]).flatMap(function(n){var t,i=(null===(t=null==n?void 0:n.text)||void 0===t?void 0:t.split(/\s+OR\s+/))||[];return i.length>1?i.map(function(t,i){return e(e({},n),{id:"split-".concat(n.id,"-").concat(i),text:t.trim()})}):[n]}),configData:i.get(n)||Bn,isReadOnly:d.is_readOnly||!1,is_variant:d.is_variant||!1,reasoning:{ruleDescription:Nr(y),whenToApply:Nr(d.wta),whenNotToApply:Nr(d.wnta),configData:Nr(d.action)}}}),n},{}),citation:ri,fieldCitations:_i,citations:li};console.log("templateState",$r);var Xr={setRuleTitle:function(n){var t,i=void 0!==(null===(t=null==n?void 0:n.target)||void 0===t?void 0:t.value)?n.target.value:n;tt(i),Ei(function(n){if(n.ruleTitle){var t=e({},n);return delete t.ruleTitle,t}return n})},setRuleDescription:function(e,n,t){return _r(e,n,t)},addWhenToApply:function(e,n){return pr(e,n)},removeWhenToApply:function(e,n,t){return fr(e,n,t)},editWhenToApply:function(e,n,t,i){return gr(e,n,t,i)},addWhenNotToApply:function(e,n){return mr(e,n)},removeWhenNotToApply:function(e,n,t){return wr(e,n,t)},editWhenNotToApply:function(e,n,t,i){return yr(e,n,t,i)},addNewVariant:function(e){return hr(e)},onScopeChange:Tr,onConfigChange:function(e,n,t,i){return Ar(e,n,t,i)}},ea={},na={};Yn.forEach(function(n,t){ea[t]=new Map,na[t]=new Map,n.forEach(function(n,i){ea[t].set(i,[e({key:"order_rules",label:"Order"},n.order_rules),e({key:"advertiser_rules",label:"Advertiser"},n.advertiser_rules)]),na[t].set(i,[{key:"inventory_rules",label:"Inventory code",enabled:n.inventory_rules.respect_inventory_code},{key:"break_code_rules",label:"Break Code",enabled:n.break_code_rules.respect_break_code},{key:"is_default_enabled",label:"Default Variant",enabled:n.is_default_enabled}])})});var ta=v(function(){if(te){if(1===fe.tiers.length)return 0;var e=fe.tiers.findIndex(function(e){return e.toLowerCase()===te.toLowerCase()});return e>=0?e:0}},[te,fe.tiers]),ia=e(e({defaultOpen:!0,tierOptions:["Tier 1","Tier 2"],stationOptions:[{label:"Global Scope",value:"global"},{label:"Genre Scope",value:"genre"},{label:"Custom Scope",value:"custom"}],separationItemsByTier:ea,togglesByTier:na,highlights:Qt,highlightedIds:$t,openSections:zn,setOpenSections:Zn,setSendMessageInChat:Dt,isAiStreaming:gi,defaultTierOpen:ta,defaultVariantOpen:gn?void 0:xt,autoScrollVariantIndex:gn?void 0:xt,activeVariantAccordionIndex:gn?void 0:xt,setActiveVariantAccordionIndex:function(e){Ot(e),null!=e&&setTimeout(function(){mn(!1)},100)},validationErrors:Ci,activeEditId:Sn,setActiveEditId:Nn,pendingEditId:En,setPendingEditId:xn,conflictReason:Oi,usedFor:ne||ie?"Edit":"Create"},Hn),{scrollTrigger:ni});console.log(xt,"editVariantIndex checking"),d(function(){},[z]),d(function(){if(!gi&&It&&!!(!ne&&!ie)){var e=n([],gt,!0).reverse().findIndex(function(e){return"variant"===e.action||e.is_variant});-1===e||gt.length,-1!==e&&(console.log("Post-stream: Focusing new variant at index",e),Ot(e))}},[gi,It]);var ra=function(){xe.current&&(clearTimeout(xe.current),xe.current=null),Ye.current=!1,Z(),Mr()},aa=function(){var e={};nt.trim()||(e.ruleTitle="Rule Title is required"),fe.station||(e.station="Station Rule Set is required"),fe.tiers&&0!==fe.tiers.length||(e.tiers="At least one tier must be selected");return fe.tiers.map(function(e){return e.toLowerCase().replace(/ /g,"_")}).forEach(function(n){var t=lt.get(n),i=dt.get(n),r=Yn.get(n);r&&Array.from(r.keys()).forEach(function(r){var a=null==t?void 0:t.get(r);a&&a.trim()||(e["ruleDescription-".concat(n,"-").concat(r)]="Rule Description is required");var o=null==i?void 0:i.get(r);o&&o.some(function(e){return""!==e.text.trim()})||(e["whenToApply-".concat(n,"-").concat(r)]='At least one "When to apply" condition is required')})}),e};function oa(r,a,o){var l,s,u,d,c,v;return void 0===o&&(o=!1),t(this,void 0,void 0,function(){var t,_,p,f,g,m,w,y,h,T,A,M,I,k,S,N,C,E,x,O,L,R,D,j;return i(this,function(i){switch(i.label){case 0:if(t=r.question_id,_=(null==r?void 0:r.variant_id)||null,Mn.current&&Vn.current===t)return console.log("[handleAILoadAction] Already fetching question ID, skipping duplicate request:",t),[2];if(Dn.current===t&&!o)return console.log("[handleAILoadAction] Question ID unchanged, updating variant focus only:",_),Rn(_),_?(-1!==(p=(gt||[]).findIndex(function(e){return e.is_new_variant}))&&(Si(!0),Bi("Removing new variant..."),Ki("Please wait while we remove new variant.")),mt(function(e){if(!Array.isArray(e))return e;var n=e.filter(function(e){return!e.is_new_variant}),t=n.findIndex(function(e){return e.variant_id===_});return-1!==t&&setTimeout(function(){Ot(t),ti(function(e){return e+1}),hi.current=t},0),n}),-1!==p&&((f=function(e){e(function(e){var n=new Map;return e.forEach(function(e,t){var i=new Map(e);i.delete(p);var r=new Map,a=0;i.forEach(function(e){r.set(a,e),a++}),n.set(t,r)}),n})})(Kn),f(st),f(ct),f(pt),qt(function(e){var t=n([],e,!0);return t.length>p&&t.splice(p,1),t})),!1===Mn.current&&(Mn.current=!0,-1!==p&&(null===(l=bi.current)||void 0===l||l.show({severity:"success",summary:"Success",detail:"New variant removed successfully because you selected to edit an existing variant",life:4e3}))),mn(!1)):(console.log("lastLoadedQuestionIdRef.current in else",Dn.current),null===Dn.current&&(Si(!0),Bi("Adding new variant..."),Ki("Please wait while we add new variant."))),[2];console.log("User Action: fetching fresh variant info for question",t),In.current.clear(),In.current.add(t),Si(!0),Bi("Loading variants..."),console.log("reaching here in load and edit"),Ki("Please wait while we fetch the latest variant data."),Mn.current=!0,Vn.current=t,i.label=1;case 1:return i.trys.push([1,5,6,10]),g=Date.now(),[4,b(t,(null==ee?void 0:ee.ruleSetId)||"")];case 2:return m=i.sent(),(w=Date.now()-g)<3e3?[4,new Promise(function(e){return setTimeout(e,3e3-w)})]:[3,4];case 3:i.sent(),i.label=4;case 4:return 0===(y=(null===(s=null==m?void 0:m.data)||void 0===s?void 0:s.variants)||[]).length?(console.warn("No variants found for question_id",t),Si(!1),Bi(""),null===(u=bi.current)||void 0===u||u.show({severity:"error",summary:"Error",detail:"No variants found for this question.",life:3e3}),[2]):(Ye.current=!0,Mn.current=!0,h=(null===(d=null==m?void 0:m.data)||void 0===d?void 0:d.ruleTitle)||"",tt(h),T=null===(c=null==m?void 0:m.data)||void 0===c?void 0:c.tier,A="tier_1",M="Tier 1",I="1",T&&((k=String(T).match(/(\d+)/))?(S=k[1],I=S,A="tier_".concat(S),M="Tier ".concat(S)):(A=String(T).toLowerCase().trim().replace(/ /g,"_"),M=A.replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()}),I=A.includes("2")?"2":"1")),console.log("[handleAILoadAction] Detected Tier: ".concat(M," (ID: ").concat(I,", Key: ").concat(A,")")),ge({station:X||"global",tiers:[M]}),N=function(e,n,t,i,r,a){var o=e.map(function(e){var o,l,s=[t];return{variant_type:e.variantType||"variant",title:n,rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:s,scheduling_config:(null===(o=e.configuration)||void 0===o?void 0:o.scheduling_config)||{},variant_id:e.variantId,question_id:r,tier_id:i,is_readOnly:e.variantId!==a,link_id:e.link_id||"",is_enabled:null===(l=e.is_enabled)||void 0===l||l,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}}),l=new Map,s=new Map,u=new Map,d=new Map;return o.forEach(function(e,n){var t,i,r,a,o,c,v=e.scheduling_config||{};l.set(n,{order_rules:{enabled:!!v.order_global_min_separation_seconds||!!(null===(t=v.order_rules)||void 0===t?void 0:t.global_min_separation_seconds),global_min_separation_seconds:Math.floor((v.order_global_min_separation_seconds||(null===(i=v.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)},advertiser_rules:{enabled:!!v.advertiser_global_min_separation_seconds||!!(null===(r=v.advertiser_rules)||void 0===r?void 0:r.global_min_separation_seconds),global_min_separation_seconds:Math.floor((v.advertiser_global_min_separation_seconds||(null===(a=v.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)},inventory_rules:{respect_inventory_code:!!v.respect_inventory_code||!!(null===(o=v.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)},break_code_rules:{respect_break_code:!!v.respect_break_code||!!(null===(c=v.break_code_rules)||void 0===c?void 0:c.respect_break_code)},is_default_enabled:U(e.variantType||e.variant_type)}),s.set(n,q(e.rule_description||""));var _=e.wta?e.wta.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];u.set(n,_);var p=e.wnta?e.wnta.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,p)}),{transformedVariants:o,tierConfigMap:l,tierDescMap:s,tierWtaMap:u,tierWntaMap:d}}(y,h,A,I,t,_),C=N.transformedVariants,E=N.tierConfigMap,x=N.tierDescMap,O=N.tierWtaMap,L=N.tierWntaMap,ht(function(e){var n=new Map(e);return n.set(A,JSON.parse(JSON.stringify(C))),n}),mt(function(e){var t=(Array.isArray(e)?e:[]).filter(function(e){return"copy"===e.action||e.tier_id&&e.tier_id!==I});return n(n([],t,!0),C,!0)}),At(function(e){var t=(Array.isArray(e)?e:[]).filter(function(e){return"copy"===e.action||e.tier_id&&e.tier_id!==I});return n(n([],t,!0),C,!0)}),An(!0),Dn.current=t,Rn(_),R=0,_&&-1!==(D=C.findIndex(function(e){return e.variant_id===_}))&&(R=D),Ot(R),ti(function(e){return e+1}),hi.current=R,mn(!1),Kn(function(e){var n=new Map(e);return n.set(A,E),n}),st(function(e){var n=new Map(e);return n.set(A,x),n}),ct(function(e){var n=new Map(e);return n.set(A,O),n}),pt(function(e){var n=new Map(e);return n.set(A,L),n}),Ei({}),[3,10]);case 5:return j=i.sent(),console.error("Failed to fetch variants by question ID:",j),null===(v=bi.current)||void 0===v||v.show({severity:"error",summary:"Error",detail:"Failed to fetch variants. Please try again.",life:3e3}),[3,10];case 6:return Mn.current=!1,Si(!1),Bi(""),Ki(""),(null==r?void 0:r.is_interpretation_ready)&&(null==r?void 0:r.question_id)&&Dn.current&&Ln?(console.log("inside edit variant flow",Dn.current,Ln),mt(function(n){if(!Array.isArray(n))return n;var t=n.map(function(n){return(null==n?void 0:n.variant_id)===Ln?e(e({},n),a):n}),i=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===Ln});return console.log(i,"targetIndexif"),-1!==i&&setTimeout(function(){console.log("targetIndex",i),Ot(i),ti(function(e){return e+1}),hi.current=i},0),t}),[3,9]):[3,7];case 7:return(null==r?void 0:r.is_interpretation_ready)&&(null==r?void 0:r.question_id)&&Dn.current&&!Ln?(console.log("inside add new variant flow",Dn.current,Ln),Si(!0),Bi("Adding new variant..."),Ki("Please wait while we add new variant."),[4,new Promise(function(e){return setTimeout(e,3e3)})]):[3,9];case 8:i.sent(),Si(!1),Bi(""),Ki(""),mt(function(t){var i;if(Array.isArray(t)){var r=t.findIndex(function(e){return!0===e.is_new_variant});if(-1!==r)setTimeout(function(){Ot(r),ti(function(e){return e+1}),hi.current=r},0),i=t.map(function(n,t){return t===r?e(e(e({},n),a),{is_new_variant:!0,is_variant:!0}):n});else{var o=t.length;setTimeout(function(){Ot(o),ti(function(e){return e+1}),hi.current=o},0),i=n(n([],t,!0),[e(e({},a),{is_new_variant:!0,is_variant:!0})],!1)}}else setTimeout(function(){Ot(0),ti(0),hi.current=0},0),i=[e(e({},a),{is_new_variant:!0})];return Fn.current=i,i}),i.label=9;case 9:return[7];case 10:return[2]}})})}var la;return console.log("RuleSet in render",ee),r(o,{children:[a(g,{ref:bi}),a(k,{open:z,chatAssistantName:Jn.ASSISTANT_NAME,organizationName:Jn.ORGANIZATION_NAME,baseUrl:Jn.BASE_URL,baseWebsocketUrl:Jn.BASE_WEBSOCKET_URL,closeFunction:ra,ThinkingBulbIconComponent:a(j,{}),customSections:a(o,{}),customMetaData:He,chatTitle:ne?Jn.CHAT_TITLE_EDIT.replace("<scope>",(null==ee?void 0:ee.ruleSetName)||""):Jn.CHAT_TITLE.replace("<scope>",(null==ee?void 0:ee.ruleSetName)||""),formTitle:Jn.FORM_TITLE,showSavedMessage:!1,savedMessage:Me,isError:Ce,errorMessage:ke,isSaving:Le,onSave:function(e){console.log("Save this Learning",e);var n=aa();if(Object.keys(n).length>0)return console.log("validation errors",n),void Ei(n);Re(!0),Ee(!1),Se("")},showSaveWithoutValidation:!1,onSaveWithoutValidation:function(){return t(void 0,void 0,void 0,function(){var e,n,t,r,a,o,l,s,u;return i(this,function(d){switch(d.label){case 0:return e=aa(),Object.keys(e).length>0?(Ei(e),[2]):(console.log("✅ Bypass Save: Initiated"),ne||ie?[4,Gr()]:[3,2]);case 1:return d.sent(),[3,11];case 2:return!0!==Ye.current?[3,4]:[4,Fr()];case 3:return d.sent(),[3,11];case 4:d.trys.push([4,10,,11]),ye(F),n=new Map,t=function(e){var t,r,a,o,l,s;return i(this,function(i){switch(i.label){case 0:if(t=e.toLowerCase().replace(/ /g,"_"),r=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===t||e.tier_name.toLowerCase()===t.replace(/_/g,"")}),!(a=null==r?void 0:r.tier_id))return n.set(t,null),[2,"continue"];o={question:nt,orgName:Jn.ORGANIZATION_NAME,scope_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a},i.label=1;case 1:return i.trys.push([1,3,,4]),[4,w(o)];case 2:return l=i.sent(),n.set(t,(null===(u=null==l?void 0:l.data)||void 0===u?void 0:u.id)||null),[3,4];case 3:return s=i.sent(),console.error("❌ Bypass Save: Error saving question for ".concat(e,":"),s),n.set(t,null),[3,4];case 4:return[2]}})},r=0,a=fe.tiers,d.label=5;case 5:return r<a.length?(o=a[r],[5,t(o)]):[3,8];case 6:d.sent(),d.label=7;case 7:return r++,[3,5];case 8:return[4,Pr(n,!0)];case 9:return d.sent(),[3,11];case 10:return l=d.sent(),ye(G),s=new Map,fe.tiers.forEach(function(e){s.set(e.toLowerCase().replace(/ /g,"_"),null)}),Pr(s,!0),console.error("❌ Bypass Save: Error",l),[3,11];case 11:return[2]}})})},headerMessageHandlers:Sr,aiResponse:gt,setAIResponse:mt,width:"90vw",height:"90vh",sendMessageInChat:Rt,template:S.CreateRule,isLoading:we===F,templateProps:{state:$r,handlers:Xr,uiState:ia},setSendMessageInChat:Dt,onResetChat:function(){Mr()},onResetRequest:function(){Mi(!0)},isFormDirty:function(){return""!==nt.trim()||""!==fe.station||fe.tiers.length>0||Array.from(lt.values()).some(function(e){return Array.from(e.values()).some(function(e){return""!==e.trim()})})||Array.from(dt.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.length>0})})||Array.from(_t.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.length>0})})||Yn.size>0||rt.size>0},initialChatMessage:ne?(la=ne.ruleTitle||"this rule",null===(Q=null==ie?void 0:ie[null!=re?re:0])||void 0===Q||Q.is_user_accepted_risk,'<p><span>Welcome back! 👋</span>\n <span>I see you want to edit <strong>"'.concat(la,'."</strong></span>\n <span> <strong>What would you like to update? </strong> </span>\n <span>Describe the change you want to make, and I’ll help you edit it.</span>\n </p>')):null!=ve?ve:Jn.INITIAL_CHAT_MESSAGE,validateCustom:function(){if(we===P||be)return!1;if(!nt||""===nt.trim())return!1;if(null!==Sn)return!1;if(!fe.station||""===fe.station.trim())return!1;var e=fe.tiers&&fe.tiers.filter(function(e){return e&&""!==e.trim()});if(!e||0===(null==e?void 0:e.length))return!1;for(var n,t=0,i=e.map(function(e){return e.toLowerCase().replace(/ /g,"_")});t<i.length;t++){var r=i[t],a=lt.get(r),o=dt.get(r),l=Yn.get(r);if(!l||0===l.size)return!1;for(var s=0,u=Array.from(l.keys());s<u.length;s++){var d=u[s],c=null==a?void 0:a.get(d);if(!c||""===c.trim())return!1;var v=null==o?void 0:o.get(d);if(!(null==v?void 0:v.some(function(e){return e.text&&""!==e.text.trim()})))return!1}}return!(!Yn||0===Yn.size)&&(!(n=function(e){return Array.from(e.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.some(function(e){return!!e.conflict})})})})(dt)&&!n(_t)&&!$e)},setCitations:si,isLocked:!0,textareaPlaceholder:"Tell me more about this learning..",saveButtonConfig:{label:ne?"Save Changes":"Save Learning",loadingLabel:r("span",e({style:{display:"flex",gap:"5px",alignItems:"center"}},{children:[a(p,{size:20,color:"white",className:"animate-spin"}),a("span",{children:" Saving Changes..."})]}))},setCurrentRequestId:ci,isAiStreaming:gi,setIsAiStreaming:mi,showConfirmReset:Ai,setShowConfirmReset:Mi,confirmReset:function(){Mr(),Mi(!1),ra()},isLoadingVariants:ki,mainText:Hi,subText:Yi,proposedRule:rn,isFetchingComparison:vr,handleOpenComparison:kr,showComparison:qi,setShowComparison:Ui,variants:ln,onComparisonLinkClick:cr,targetComparisonVariantId:dn,highlightedComparisonField:_n,onAddAsVariant:function(){Ui(!1)},isCopyEnabled:!0,onSubmitFeedback:function(n){return t(void 0,void 0,void 0,function(){var t,r,a,o,l;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),t=e({user_id:null!==(a=null==lr?void 0:lr._id)&&void 0!==a?a:"",user_name:null!==(o=null==lr?void 0:lr.firstname)&&void 0!==o?o:"",user_email:null!==(l=null==lr?void 0:lr.email)&&void 0!==l?l:"",assistant_name:Jn.ASSISTANT_NAME},n),[4,T(t)];case 1:return i.sent(),[3,3];case 2:throw r=i.sent(),console.error("Failed to submit chat feedback",r),r;case 3:return[2]}})})},handleGlobalClickCapture:function(e){null!==Sn&&(e.target.closest('[data-edit-id="'.concat(Sn,'"]'))||(e.preventDefault(),e.stopPropagation(),xn(Sn)))},isGlobalEditActive:null!==Sn,initialDraftMessage:se?ce:"",socketTimeOutInMilliSeconds:Jn.SOCKET_TIMEOUT})]})};export{H as default,q as filterAgentText};
1
+ import{__assign as e,__spreadArray as n,__awaiter as t,__generator as i}from"tslib";import{jsxs as r,jsx as a,Fragment as o}from"react/jsx-runtime";import l,{useState as s,useRef as u,useEffect as d,useCallback as c,useMemo as _}from"react";import{AlertTriangle as v,Loader as p,Check as f}from"lucide-react";import{Toast as g}from"primereact/toast";import{v4 as m}from"uuid";import{saveInstantLearningQuestion as w,publishFeedback as y,updateVariant as h,fetchRulesByQuestionId as b,submitChatFeedback as A}from"../apis/scopeApis.js";import T from"../public/ThinkingBulbIcon.gif.js";import{useLearningTemplateStore as M}from"../store/useLearningTemplateStore.js";import I from"../styles/InstantLearningModal.module.css.js";import k from"../../IL-OTJ/ILOTJ.js";import"../../IL-OTJ/_components/atoms/Textarea/Textarea.js";import"../../IL-OTJ/_components/InputDataReasoningCard.js";import"../../IL-OTJ/_components/TooltipComponent.js";import{TemplateList as S}from"../../IL-OTJ/_components/templates/TemplatesMap/templatesMap.js";import{extractAndParseWithProperty as C,parseValidationData as N,extractAndParseResponse as x,extractVariantIds as E}from"../../IL-OTJ/helpers/utils.js";import{useCitations as O}from"../../IL-OTJ/store/CitationsContext.js";import"@mui/material";import"@mui/material/Tooltip";import"react-markdown";import"rehype-raw";import"axios";import"@e-llm-studio/streaming-response";import"@e-llm-studio/watch-me-work";import"../../IL-OTJ/_components/ChatComponents/ChatHeader.js";import"remark-gfm";import"../../IL-OTJ/_components/MarkdownComponents/MarkdownElements.js";import"../../IL-OTJ/_components/CitationLink.js";import"@e-llm-studio/citation";import"../../IL-OTJ/_components/templates/RuleComparison/RuleComparison.js";import"./ToolTip.js";import L from"../store/useAuthStore.js";import"date-fns";import"framer-motion";import"@mui/icons-material/Close";import{useConfigStore as R}from"../store/useConfigStore.js";import{IL_CONFIG_CONSTANTS as D}from"../config.js";import"@radix-ui/react-tooltip";import"./StationOnboarding/InputField.js";import"../store/useEnvStore.js";import"react-router-dom";var j=function(){return a("img",{src:T,alt:"Thinking...",width:"20",height:"20"})},V="INITIAL",P="SUCCESS",G="FAILURE",F="IN_PROGRESS",q=function(e){return"string"!=typeof e?"":e.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,"").trim()},U=function(e){if(!e)return!1;var n=e.toLowerCase().trim();return"default"===n||"default_variant"===n},J=function(e){return e.toLowerCase().replace(/ /g,"_").replace(/tier(\d+)/i,"tier_$1")},H=function(T){var H,B,W,K,Y,z,Q=T.isOpen,Z=T.onClose,$=T.initialScope,X=void 0===$?"global":$,ee=T.ruleSet,ne=T.editingRule,te=T.editingTier,ie=T.editingVariants,re=T.focusedVariantIndex,ae=T.setFocusedVariantIndex,oe=T.triggerAddVariant,le=T.triggerFixGaps,se=T.copiedRule,ue=T.copiedSourceTier,de=T.copiedTargetTier,ce=T.initialDraftMessage,_e=T.initialChatMessage,ve=T.applicationIntegrationId;T.modeId;var pe=s({station:X,tiers:["Tier 1"]}),fe=pe[0],ge=pe[1],me=s(V),we=me[0],ye=me[1],he=s(!1),be=he[0],Ae=he[1],Te=s("Thank you teacher for teaching me, your rule has been saved successfully."),Me=Te[0];Te[1];var Ie=s(""),ke=Ie[0],Se=Ie[1],Ce=s(!1),Ne=Ce[0],xe=Ce[1],Ee=u(null),Oe=s(!1),Le=Oe[0],Re=Oe[1],De=u(!1),je=M().tiers,Ve=l.useRef(new Set),Pe=s(null);Pe[0];var Ge=Pe[1],Fe=s(null);Fe[0];var qe=Fe[1],Ue=s(null);Ue[0];var Je=Ue[1],He=u(new Map),Be=u(""),We=u(!1),Ke=s(0),Ye=Ke[0],ze=Ke[1],Qe=s(!1),Ze=Qe[0],$e=Qe[1],Xe=u(null),en=u(0),nn=s(null),tn=nn[0],rn=nn[1],an=s([]),on=an[0],ln=an[1],sn=s(null),un=sn[0],dn=sn[1],cn=s(null),_n=cn[0],vn=cn[1],pn=s(!1),fn=pn[0],gn=pn[1],mn=u({}),wn=u([]),yn=u(!1),hn=s(!1),bn=hn[0],An=hn[1],Tn=u(!1),Mn=u(new Set),In=s(null),kn=In[0],Sn=In[1],Cn=s(null),Nn=Cn[0],xn=Cn[1],En=s(null),On=En[0],Ln=En[1],Rn=u(null),Dn=u(null),jn=u(null),Vn=u({}),Pn=u([]),Gn=u([]),Fn=u({}),qn=R(),Un=qn.configuration,Jn=qn.ilLabels;d(function(){kn||xn(null)},[kn]),d(function(){var e;if(Q&&ne){Rn.current=ne.ruleId;var n=null===(e=null==ie?void 0:ie[re||0])||void 0===e?void 0:e.variantId;Ln(n||null)}},[Q,ne,ie,re]),d(function(){Q&&ge(function(n){return e(e({},n),{station:X})})},[Q,X]),d(function(){console.log("triggered",On),console.log(ft,"structuredAIResponse"),gt(function(n){return Array.isArray(n)?n.map(function(n){return e(e({},n),{is_readOnly:n.variant_id!==On&&n.new_variant_id!==On})}):n}),At(function(n){return Array.isArray(n)?n.map(function(n){return e(e({},n),{is_readOnly:n.variant_id!==On&&n.new_variant_id!==On})}):n})},[On]),d(function(){var n;if(!Q)return Tr(),void(yn.current=!1);if(!yn.current){var t=ie,i=t&&t.length>0,r=null!=ne;if(i||r){if((null==ne?void 0:ne.ruleTitle)&&nt(ne.ruleTitle),te&&(ge(function(n){return e(e({},n),{tiers:[te]})}),Dn.current=te.toLowerCase().replace(/ /g,"_")),i){var a=te?J(te):"tier_1",o=new Map,l=new Map,s=new Map,u=new Map,d=new Map,c=new Map,_=[];t.forEach(function(e,n){u.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];c.set(n,i),_.push(e.variantId||e.variant_id||"")}),o.set(a,u),l.set(a,d),s.set(a,c);var v=null===(n=null==t?void 0:t[re||0])||void 0===n?void 0:n.variantId,p=t.map(function(e,n){var t,i,r=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config;return{variant_type:e.variantType||"variant",title:(null==ne?void 0:ne.ruleTitle)||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[a],scheduling_config:r||{},variant_id:e.variantId,question_id:(null==ne?void 0:ne.ruleId)||"",tier_id:a.replace("tier_",""),is_readOnly:e.variantId!==v,link_id:e.link_id||"",is_enabled:null===(i=e.is_enabled)||void 0===i||i,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}});gt(p),gt(p),At(p),yt(function(e){var n=new Map(e);return n.set(a,JSON.parse(JSON.stringify(p))),n});var f=new Map;t.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,_,v,p,g=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,m=null!==(a=null!==(i=null==g?void 0:g.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==g?void 0:g.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,w=null!==(s=null!==(o=null==g?void 0:g.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==g?void 0:g.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;f.set(n,{order_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},advertiser_rules:{enabled:w>0,global_min_separation_seconds:Math.floor(w/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==g?void 0:g.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==g?void 0:g.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(p=null!==(v=null===(_=null==g?void 0:g.break_code_rules)||void 0===_?void 0:_.respect_break_code)&&void 0!==v?v:null==g?void 0:g.respect_break_code)&&void 0!==p&&p},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[a,f]])),lt(o),dt(l),vt(s)}else if(r&&ne){var g=ne.ruleDesc;if(g&&lt(new Map([["tier_1",new Map([[0,g]])]])),ne.whenToApply){var m=ne.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,n){return{id:"edit-wta-".concat(n),text:e,reasoning:""}});dt(new Map([["tier_1",new Map([[0,m]])]]))}if(ne.whenNotToApply){var w=ne.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,n){return{id:"edit-wnta-".concat(n),text:e,reasoning:""}});vt(new Map([["tier_1",new Map([[0,w]])]]))}}}else if(se&&ue&&de){nt(se.ruleTitle||""),ge(function(n){return e(e({},n),{tiers:[de]})}),Dn.current=J(de);var y=J(ue),h=se.variants||[],b=(o=new Map,l=new Map,s=new Map,new Map),A=new Map,T=new Map;h.forEach(function(e,n){b.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e,t){return{id:"copy-wta-".concat(n,"-").concat(t),text:e.trim(),reasoning:""}}):[];A.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e,t){return{id:"copy-wnta-".concat(n,"-").concat(t),text:e.trim(),reasoning:""}}):[];T.set(n,i)}),o.set(y,b),l.set(y,A),s.set(y,T),lt(o),dt(l),vt(s);var M=h.map(function(e,n){var t,i,r=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config;return{variant_type:e.variantType||"variant",title:se.ruleTitle||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[ue],scheduling_config:r||{},variant_id:e.variantId,question_id:se.ruleId||"",tier_id:y.replace("tier_",""),is_readOnly:!0,link_id:e.link_id||"",is_enabled:null===(i=e.is_enabled)||void 0===i||i,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"copy"}});gt(M),At(M),yt(function(e){var n=new Map(e);return n.set(y,JSON.parse(JSON.stringify(M))),n});var I=new Map;h.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,_,v,p,f=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,g=null!==(a=null!==(i=null==f?void 0:f.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==f?void 0:f.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,m=null!==(s=null!==(o=null==f?void 0:f.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==f?void 0:f.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;I.set(n,{order_rules:{enabled:g>0,global_min_separation_seconds:Math.floor(g/60)},advertiser_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==f?void 0:f.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==f?void 0:f.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(p=null!==(v=null===(_=null==f?void 0:f.break_code_rules)||void 0===_?void 0:_.respect_break_code)&&void 0!==v?v:null==f?void 0:f.respect_break_code)&&void 0!==p&&p},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[y,I]]))}}},[Q,ne,ie,te,se,ue,de]),d(function(){if(Q){if(console.log("[Accordion Init] editingTier:",te,"focusedVariantIndex:",re),te&&ge(function(n){return e(e({},n),{tiers:[te]})}),fn)return console.log("[Accordion] isAddingNewVariant mode — closing all variant accordions"),void Et(void 0);if(oe){console.log("[Accordion] triggerAddVariant mode — not opening any variant on RHS"),Et(void 0);var n=(te?J(te):"tier_1").replace(/_/g," ");Rt("I want to add a new variant to ".concat(n))}else null!=re?(console.log("[Accordion] Opening SPECIFIC variant index ".concat(re," (from VariantCard)")),Et(re),ni(function(e){return e+1})):(ne||ie&&ie.length>0)&&(console.log("[Accordion] Opening DEFAULT/1st variant (index 0) for RuleHeader edit"),Et(0),ni(function(e){return e+1}))}else Et(void 0)},[Q,te,re,ne,ie,oe,fn]),d(function(){Q&&le&&ne&&null!==re&&Rt("I want to fix the gaps")},[Q,le,ne,re]);var Hn={order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1},is_default_enabled:!1},Bn=s(new Map([["tier_1",new Map([[0,Hn]])],["tier_2",new Map([[0,Hn]])]])),Wn=Bn[0],Kn=Bn[1],Yn=s(new Set([])),zn=Yn[0],Qn=Yn[1],Zn=s("");Zn[0];var $n=Zn[1],Xn=s(""),et=Xn[0],nt=Xn[1],tt=s(new Map([["tier_1",new Map],["tier_2",new Map]])),it=tt[0],rt=tt[1],at=s(new Map([["tier_1",new Map],["tier_2",new Map]])),ot=at[0],lt=at[1],st=s(new Map([["tier_1",new Map],["tier_2",new Map]])),ut=st[0],dt=st[1],ct=s(new Map([["tier_1",new Map],["tier_2",new Map]])),_t=ct[0],vt=ct[1],pt=s([]),ft=pt[0],gt=pt[1],mt=s(new Map),wt=mt[0],yt=mt[1],ht=s([]),bt=ht[0],At=ht[1],Tt=s(!1),Mt=Tt[0],It=Tt[1],kt=s([]),St=kt[0],Ct=kt[1],Nt=s(void 0),xt=Nt[0],Et=Nt[1],Ot=s(""),Lt=Ot[0],Rt=Ot[1],Dt=s(null),jt=Dt[0],Vt=Dt[1],Pt=s([]),Gt=Pt[0],Ft=Pt[1],qt=s([]),Ut=qt[0],Jt=qt[1],Ht=s({}),Bt=Ht[0],Wt=Ht[1],Kt=s(new Map),Yt=Kt[0],zt=Kt[1],Qt=s(new Set),Zt=Qt[0],$t=Qt[1],Xt=s(0),ei=Xt[0],ni=Xt[1],ti=s({ruleTitle:"",ruleDescription:"",scopeData:"",whenToApply:"",whenNotToApply:"",configData:""}),ii=ti[0],ri=ti[1],ai=s([]),oi=ai[0],li=ai[1],si=s(""),ui=si[0],di=si[1],ci=s({}),_i=ci[0],vi=ci[1],pi=s(!1),fi=pi[0],gi=pi[1],mi=u(null),wi=u(null),yi=u(-1),hi=u(null);d(function(){Gn.current=ft},[ft]),d(function(){var n=new Set,t=console.warn;return console.warn=function(){for(var i,o=[],l=0;l<arguments.length;l++)o[l]=arguments[l];t.apply(void 0,o);var s=o[0];if("string"==typeof s&&s.includes("Failed to parse JSON for property=")){var u=s.match(/warnId="([^"]+)"/),d=null==u?void 0:u[1];if(d&&n.has(d))return;d&&n.add(d),null===(i=hi.current)||void 0===i||i.show({severity:"warn",sticky:!1,life:5e3,className:I.toastWarning,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(v,{className:I.iconWarning}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleWarning)},{children:"Parsing Issue"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageWarning)},{children:"The AI response couldn't be parsed correctly. Please try rephrasing your input or retry."}))]}))})}},function(){console.warn=t}},[]);var bi=s(!1),Ai=bi[0],Ti=bi[1],Mi=s(!1),Ii=Mi[0],ki=Mi[1],Si=s({}),Ci=Si[0],Ni=Si[1],xi=s(""),Ei=xi[0],Oi=xi[1],Li=s(!0),Ri=Li[0],Di=Li[1],ji=s(!1),Vi=ji[0],Pi=ji[1],Gi=s(!1),Fi=Gi[0],qi=Gi[1],Ui=s(""),Ji=Ui[0],Hi=Ui[1],Bi=s(""),Wi=Bi[0],Ki=Bi[1],Yi=s(""),zi=Yi[0],Qi=Yi[1],Zi=s(null),$i=Zi[0],Xi=Zi[1],er=s(null),nr=er[0],tr=er[1],ir=s(null),rr=ir[0],ar=ir[1],or=L.getState().user,lr=O(),sr=lr.dispatch,ur=lr.getLatestReasoning;d(function(){console.log("Structured AI Response updated:",ft)},[ft]),d(function(){console.log("isCloseModal AI Response updated:",Ri)},[Ri]),d(function(){!1===fi?(ki(!1),Hi(""),Ki("")):(xe(!1),Se(""))},[fi]),d(function(){null!=xt&&ie&&!ie[xt]&&Xi(null)},[xt,ie]),d(function(){if(Le&&!De.current){De.current=!0,console.log("UseEffect triggered: Sending save message to chat agent");Rt("Save this Learning"),Re(!1),setTimeout(function(){De.current=!1,Rt("")},2e3)}},[Le]);var dr=c(function(e,n){on.some(function(n){return n.id===e})?(qi(!0),dn(e),vn(n),setTimeout(function(){vn(null),dn(null)},6e3)):console.warn("Variant ID ".concat(e," not found in comparison variants."))},[on]),cr=_(function(){var e=Fi&&Ye>0;return console.log("[Loader Debug] showComparison: ".concat(Fi,", activeRequests: ").concat(Ye,", Result: ").concat(e)),e},[Fi,Ye]),_r=c(function(n,t,i){var r,a=void 0!==(null===(r=null==i?void 0:i.target)||void 0===r?void 0:r.value)?i.target.value:i;lt(function(e){var i=new Map(e),r=new Map(i.get(n));return r.set(t,a),i.set(n,r),i}),Ni(function(i){if(i["ruleDescription-".concat(n,"-").concat(t)]){var r=e({},i);return delete r["ruleDescription-".concat(n,"-").concat(t)],r}return i})},[]),vr=c(function(t,i){dt(function(e){var r=new Map(e),a=new Map(r.get(t)),o=a.get(i)||[];return a.set(i,n(n([],o,!0),[{id:Date.now(),text:"",reasoning:""}],!1)),r.set(t,a),r}),Ni(function(n){if(n["whenToApply-".concat(t,"-").concat(i)]){var r=e({},n);return delete r["whenToApply-".concat(t,"-").concat(i)],r}return n})},[]),pr=c(function(e,n,t){dt(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(n)||[];return a.set(n,o.filter(function(e){return e.id!==t})),r.set(e,a),r})},[]),fr=c(function(n,t,i,r){dt(function(a){var o=new Map(a),l=new Map(o.get(n)),s=l.get(t)||[];return l.set(t,s.map(function(n){return String(n.id)===String(i)?e(e({},n),{text:r}):n})),o.set(n,l),o}),Ni(function(i){if(i["whenToApply-".concat(n,"-").concat(t)]){var r=e({},i);return delete r["whenToApply-".concat(n,"-").concat(t)],r}return i})},[]),gr=c(function(e,t){vt(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(t)||[];return a.set(t,n(n([],o,!0),[{id:Date.now(),text:"",reasoning:""}],!1)),r.set(e,a),r})},[]),mr=c(function(e,n,t){vt(function(i){var r=new Map(i),a=new Map(r.get(e)),o=a.get(n)||[];return a.set(n,o.filter(function(e){return e.id!==t})),r.set(e,a),r})},[]),wr=c(function(n,t,i,r){vt(function(a){var o=new Map(a),l=new Map(o.get(n)),s=l.get(t)||[];return l.set(t,s.map(function(n){return String(n.id)===String(i)?e(e({},n),{text:r}):n})),o.set(n,l),o})},[]),yr=c(function(e){var n=e.replace(/_/g," ");gn(!0),null==ae||ae(null),Et(void 0),Rt("I want to add a new variant to ".concat(n))},[]),hr=function(n,t,i){var r="".concat(n,"_").concat(t);console.log("Highlighting ".concat(i," at ").concat(r)),zt(function(n){var t,a=new Map(n),o=a.get(r)||{};return a.set(r,e(e({},o),((t={})[i]=!0,t))),a}),setTimeout(function(){zt(function(n){var t,a=new Map(n),o=a.get(r)||{};return a.set(r,e(e({},o),((t={})[i]=!1,t))),a})},1500)},br=c(function(n,t){ge(function(i){var r;return e(e({},i),((r={})[n]=t,r))}),Ni(function(t){if(t[n]){var i=e({},t);return delete i[n],i}return t})},[]),Ar=c(function(n,t,i,r){rt(function(e){var r=new Map(e),a=new Map(r.get(n)||new Map),o=a.get(t)||new Set;return o.add(i),a.set(t,o),r.set(n,a),r}),Kn(function(a){var o,l,s,u=new Map(a),d=new Map(u.get(n)||new Map),c=d.get(t)||Hn;switch(i){case"order_rules":case"advertiser_rules":s="boolean"==typeof r?e(e({},c),((o={})[i]=e(e({},c[i]),{enabled:r}),o)):"object"==typeof r&&null!==r?e(e({},c),((l={})[i]={enabled:r.enabled,global_min_separation_seconds:r.global_min_separation_seconds},l)):c;break;case"inventory_rules":s=e(e({},c),{inventory_rules:{respect_inventory_code:"boolean"==typeof r?r:r.respect_inventory_code}});break;case"break_code_rules":s=e(e({},c),{break_code_rules:{respect_break_code:"boolean"==typeof r?r:r.respect_break_code}});break;case"is_default_enabled":s=e(e({},c),{is_default_enabled:"boolean"==typeof r?r:!!r});break;default:s=c}return d.set(t,s),u.set(n,d),u})},[]);d(function(){fi&&(qi(!1),vn(null),dn(null))},[fi]);var Tr=c(function(n){var t;if(void 0===n&&(n=!1),n||(sr({type:"RESET"}),mi.current=null,gt([]),Ct([]),li([]),vi({}),qi(!1),Ve.current.clear()),nt(""),Ft([]),gn(!1),Be.current="",Qi(""),tr(null),Jt([]),ge({station:"",tiers:[]}),ze(0),$e(!1),ar(null),lt(new Map([["tier_1",new Map],["tier_2",new Map]])),dt(new Map([["tier_1",new Map],["tier_2",new Map]])),vt(new Map([["tier_1",new Map],["tier_2",new Map]])),Kn(new Map([["tier_1",new Map([[0,Hn]])],["tier_2",new Map([[0,Hn]])]])),rt(new Map([["tier_1",new Map],["tier_2",new Map]])),$n(""),Et(0),ni(0),ge({station:X,tiers:["Tier 1"]}),n||Wt({}),rt(new Map([["tier_1",new Map],["tier_2",new Map]])),Rt(""),ri({ruleTitle:"",ruleDescription:"",scopeData:"",whenToApply:"",whenNotToApply:"",configData:""}),Ni({}),Oi(""),Di(!0),Ge(null),qe(null),yt(new Map),At([]),An(!1),Je(null),wi.current=null,We.current=!1,mn.current={},Mn.current.clear(),Rn.current=null,Dn.current=null,Ln(null),Se(""),ye(V),Ae(!1),xe(!1),Re(!1),rn(null),ln([]),dn(null),vn(null),Sn(null),It(!1),Vt(null),Hi(""),Ki(""),ki(!1),Qn(new Set),De.current=!1,He.current.clear(),en.current=0,Xe.current=null,jn.current=null,yi.current=-1,Tn.current=!1,yn.current=!1,ne){ne.ruleTitle&&nt(ne.ruleTitle),te&&(ge(function(n){return e(e({},n),{tiers:[te]})}),Dn.current=te.toLowerCase().replace(/ /g,"_")),Rn.current=ne.ruleId;var i=null===(t=null==ie?void 0:ie[re||0])||void 0===t?void 0:t.variantId;if(Ln(i||null),ie&&ie.length>0){var r=te?J(te):"tier_1",a=new Map,o=new Map,l=new Map,s=new Map,u=new Map,d=new Map;ie.forEach(function(e,n){s.set(n,e.description||"");var t=e.whenToApply?e.whenToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];u.set(n,t);var i=e.whenNotToApply?e.whenNotToApply.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,i)}),a.set(r,s),o.set(r,u),l.set(r,d),lt(a),dt(o),vt(l);var c=ie.map(function(e){var n,t,a=null===(n=e.configuration)||void 0===n?void 0:n.scheduling_config;return{variant_type:e.variantType||"variant",title:ne.ruleTitle||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:[r],scheduling_config:a||{},variant_id:e.variantId,question_id:ne.ruleId||"",tier_id:r.replace("tier_",""),is_readOnly:e.variantId!==i,link_id:e.link_id||"",is_enabled:null===(t=e.is_enabled)||void 0===t||t,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}});gt(c),At(c),yt(function(e){var n=new Map(e);return n.set(r,JSON.parse(JSON.stringify(c))),n});var _=new Map;ie.forEach(function(e,n){var t,i,r,a,o,l,s,u,d,c,v,p,f,g=null===(t=e.configuration)||void 0===t?void 0:t.scheduling_config,m=null!==(a=null!==(i=null==g?void 0:g.order_global_min_separation_seconds)&&void 0!==i?i:null===(r=null==g?void 0:g.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)&&void 0!==a?a:0,w=null!==(s=null!==(o=null==g?void 0:g.advertiser_global_min_separation_seconds)&&void 0!==o?o:null===(l=null==g?void 0:g.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)&&void 0!==s?s:0;_.set(n,{order_rules:{enabled:m>0,global_min_separation_seconds:Math.floor(m/60)},advertiser_rules:{enabled:w>0,global_min_separation_seconds:Math.floor(w/60)},inventory_rules:{respect_inventory_code:null!==(c=null!==(d=null===(u=null==g?void 0:g.inventory_rules)||void 0===u?void 0:u.respect_inventory_code)&&void 0!==d?d:null==g?void 0:g.respect_inventory_code)&&void 0!==c&&c},break_code_rules:{respect_break_code:null!==(f=null!==(p=null===(v=null==g?void 0:g.break_code_rules)||void 0===v?void 0:v.respect_break_code)&&void 0!==p?p:null==g?void 0:g.respect_break_code)&&void 0!==f&&f},is_default_enabled:U(e.variantType||e.variant_type)})}),Kn(new Map([[r,_]])),Et(null!=re?re:0)}}},[sr,X,ne,te,ie,re]),Mr=function(e){if(!e||0===e.length)return"variant 1";var n=e.filter(function(e){var n=e.variant_type||e.variantType||"";return""!==n&&null!==n});return"variant ".concat(n.length+1)},Ir=c(function(){console.log("variantIds",Gt),qi(!0)},[Gt]),kr={"Let me connect the dots for you...":function(e){if("string"==typeof e){var n=e.match(/<!--\s*(\{[^}]*"variant_index"[^}]*\})\s*-->/);if(n)try{var t=JSON.parse(n[1]),i=parseInt(t.variant_index,10);if(!isNaN(i)&&i>0){var r=i-1;yi.current!==r&&(console.log("Opening variant accordion: AI index ".concat(i," → 0-based index ").concat(r)),Et(r),yi.current=r)}}catch(e){console.error("Failed to parse variant_index from HTML comment:",e)}}},"Crafted Instant Learning with C.A.R.E.":function(t){var i,r=C(t),a=C(t,"general_details");console.log("--- [CARE] Staging Initial Structure ---",r);var o=C(t,"critical_gaps");if(console.log("isCriticalGap",o),$e(!!o),r){if(r.variant_type&&!("is_variant"in r)){var l=null===(i=Cr(r.variant_type||"").toLowerCase().match(/^(default_variant|default|variant)/i))||void 0===i?void 0:i[0];r.variant_type=U(l)?"Default":"variant",r.isDefault=U(l),console.log("parsed.variant_type",r.variant_type)}var s=r.scope&&r.scope.length>0?String(r.scope[0]):null,u=s?Cr(s).toLowerCase().trim().replace(/ /g,"_"):null,d=Boolean(u&&Dn.current!==u);if(d&&u){var c=Dn.current;console.log("[Tier Switch Detected]",Dn.current,"→",u),Dn.current=u,Rn.current=null;var _=(Gn.current||[]).findIndex(function(e){return!0===(null==e?void 0:e.is_new_variant)});-1===_&&void 0!==xt&&(_=xt),c&&-1!==_&&(console.log("[Tier Switch] Stripping draft variant at index ".concat(_," from ").concat(c)),lt(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(_),n.set(c,t),n}),dt(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(_),n.set(c,t),n}),vt(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(_),n.set(c,t),n}),Kn(function(e){var n=new Map(e),t=new Map(n.get(c)||new Map);return t.delete(_),n.set(c,t),n}),gt(function(t){if(!Array.isArray(t))return t;var i=n([],t,!0);return i[_]&&(i[_]=e(e({},i[_]),{scope:[u],tier_id:u.replace("tier_","")})),Gn.current=i,i})),yi.current=-1;var v=u.replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()});ge(function(n){return e(e({},n),{tiers:[v]})})}Vt(r),Ge({Updated_Fields_Response:r,General_Response:a}),(null==a?void 0:a.variant_id)&&"new_variant_id"===(null==a?void 0:a.variant_id)&&(a.variant_id=null),(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&oa(a,r,d),(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&Rn.current&&On?gt(function(n){if(!Array.isArray(n))return n;var t=n.map(function(n){return(null==n?void 0:n.variant_id)&&n.variant_id===On?e(e({},n),r):n}),i=n.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===On});return console.log(i,"targetIndexif"),-1!==i&&setTimeout(function(){console.log("targetIndex",i),Et(i),ni(function(e){return e+1}),yi.current=i},0),t}):(null==a?void 0:a.is_interpretation_ready)&&(null==a?void 0:a.question_id)&&Rn.current&&!On?gt(function(t){var i;if(Array.isArray(t)){var a=t.findIndex(function(e){return!0===e.is_new_variant});if(-1!==a)setTimeout(function(){Et(a),ni(function(e){return e+1}),yi.current=a},0),i=t.map(function(n,t){return t===a?e(e(e({},n),r),{is_new_variant:!0,is_variant:!0}):n});else{var o=t.length;setTimeout(function(){Et(o),ni(function(e){return e+1}),yi.current=o},0),i=n(n([],t,!0),[e(e({},r),{is_new_variant:!0})],!1)}}else setTimeout(function(){Et(0),ni(function(e){return e+1}),yi.current=0},0),i=[e(e({},r),{is_new_variant:!0})];return Gn.current=i,i}):(null==a?void 0:a.is_interpretation_ready)&&(gt([r]),setTimeout(function(){Et(0),ni(function(e){return e+1}),yi.current=0},0)),void 0!==r.is_user_accepted_risk&&gt(function(t){var i=!(!ne&&!ie),a=Array.isArray(t)?n([],t,!0):[];if(i&&0===a.length&&ie&&ie.length>0&&(a=ie.map(function(e,n){var t;return{variant_type:e.variantType||"variant ".concat(n+1),title:(null==ne?void 0:ne.ruleTitle)||"",rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",is_valid:!0,is_readOnly:e.variantId!==On,variant_id:e.variantId,tier_id:(null===(t=e.tier)||void 0===t?void 0:t.tier_id)||"1",is_user_accepted_risk:e.is_user_accepted_risk||!1}})),i){var o=$i||nr,l=a.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===o||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===o||(null==e?void 0:e.id)&&e.id===o});if(-1===l&&void 0!==xt&&(l=xt),-1!==l)return a[l]=e(e({},a[l]),{is_user_accepted_risk:!!r.is_user_accepted_risk}),a}return t})}},"RHS Validation Agent Response":function(t){var i,r,a,o=t;if("string"==typeof t)try{o=JSON.parse(t)}catch(e){o=N(t)}if(qe(o),console.log("--- [RHS] Updating Staged Logic ---"),console.log("parsed data from RHS Validation",o),o){Vt(function(n){var t=n;if(!t){console.log("⚠️ No staged response found! Constructing fallback from current UI state...");var i=null;if(Array.isArray(ft)){var r=$i||nr,a=-1;!(!ne&&!ie)&&r&&(a=ft.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===r||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===r||(null==e?void 0:e.id)&&e.id===r})),-1===a&&void 0!==xt&&(a=xt),-1!==a&&(i=ft[a])}t=i?e({},i):{wta:"",wnta:""}}return e(e({},t),{wta:t.wta,wnta:t.wnta,is_valid:o.is_valid,conflictingConditions:o.conflictingConditions||[],conflictMarkdown:{},conflict_view:o.conflict_view||{}})});var l=(te||"Tier 1").toLowerCase().replace(/ /g,"_"),s=function(){var e,n=Gn.current;if(Array.isArray(n)&&n.length>0){var t,i=n.findIndex(function(e){return!0===(null==e?void 0:e.is_new_variant)});if(-1!==i)return console.log("[RHS] Resolved via is_new_variant flag:",i),i;if(On)if(-1!==(t=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===On||(null==e?void 0:e.new_variant_id)===On})))return t;if($i)if(-1!==(t=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===$i||(null==e?void 0:e.new_variant_id)===$i})))return t;var r=null!==(e=null==ie?void 0:ie.length)&&void 0!==e?e:0;if(n.length>r){var a=n.length-1;return console.log("[RHS] Resolved via length diff (new variant appended):",a),a}}var o=void 0!==xt?xt:0;return console.log("[RHS] Fallback index used:",o),o}();if(console.log("Resolved variant index for conflict injection:",s),!1===o.is_valid)((null===(i=o.conflictingConditions)||void 0===i?void 0:i.length)||0)>0&&(Ni(function(n){var t=e({},n);return o.conflictingConditions.forEach(function(e){e.condition_wnta&&!e.condition_wta?t["whenNotToApply-".concat(l,"-").concat(s)]=e.reason:t["whenToApply-".concat(l,"-").concat(s)]=e.reason}),t}),Oi("")),o.conflict_view&&((null===(r=o.conflict_view.wta)||void 0===r?void 0:r.length)>0&&dt(function(t){var i=new Map(t),r=new Map(i.get(l)||new Map),a=n([],r.get(s)||[],!0);return o.conflict_view.wta.forEach(function(n){var t=a.findIndex(function(e){return e.text.trim()===n.value});-1!==t?a[t]=e(e({},a[t]),{conflict:n}):a.push({id:"conflict-wta-".concat(Date.now(),"-").concat(Math.random()),text:n.value,conflict:n})}),r.set(s,a),i.set(l,r),i}),(null===(a=o.conflict_view.wnta)||void 0===a?void 0:a.length)>0&&vt(function(t){var i=new Map(t),r=new Map(i.get(l)||new Map),a=n([],r.get(s)||[],!0);return o.conflict_view.wnta.forEach(function(n){var t=a.findIndex(function(e){return e.text.trim()===n.value});-1!==t?a[t]=e(e({},a[t]),{conflict:n}):a.push({id:"conflict-wnta-".concat(Date.now(),"-").concat(Math.random()),text:n.value,conflict:n})}),r.set(s,a),i.set(l,r),i}));else!0===o.is_valid&&(Ni({}),Oi(""),dt(function(n){var t=new Map(n),i=new Map(t.get(l)||new Map),r=i.get(s)||[];return i.set(s,r.map(function(n){return e(e({},n),{conflict:void 0})})),t.set(l,i),t}),vt(function(n){var t=new Map(n),i=new Map(t.get(l)||new Map),r=i.get(s)||[];return i.set(s,r.map(function(n){return e(e({},n),{conflict:void 0})})),t.set(l,i),t}))}},"User Action Execution":function(e){console.log("--- [User Action] Execution ---",e);var n=x(e);(console.log("parsedaction",n),"load"===(null==n?void 0:n.action)&&(null==n?void 0:n.question_id)&&oa(n,null),"save"===(null==n?void 0:n.action)&&(null==n?void 0:n.is_save)&&(Di(!0),Pi(!0)),"discard_rule"===(null==n?void 0:n.action))&&(gt([{variant_id:void 0,rule_description:"",wta:"",wnta:"",variant_type:"variant"}]),At([]),Ln(null),Et(0),ni(0),Sn(null),An(!1),gn(!1),Ft([]),Rn.current=null,nt(""),ar(null),Kn(new Map([["tier_1",new Map([[0,Hn]])],["tier_2",new Map([[0,Hn]])]])),lt(new Map([["tier_1",new Map],["tier_2",new Map]])),dt(new Map([["tier_1",new Map],["tier_2",new Map]])),vt(new Map([["tier_1",new Map],["tier_2",new Map]])))},"Validation Agent Response":function(t){var i,r;if(console.log("--- [Validation Agent] Committing Staged Data to UI ---"),console.log("raw data from Validation Agent",t,typeof t),"string"==typeof t){var a=t.match(/<!--\s*```json\s+property="rule_validation_card"\s+([\s\S]*?)```\s*-->/i);if(a&&a[1])try{var o=JSON.parse(a[1].trim()),l=null===(r=null===(i=null==o?void 0:o.validated_rules)||void 0===i?void 0:i[0])||void 0===r?void 0:r.rule_id;l&&(console.log("Setting default comparison ID:",l),dn(l))}catch(e){console.error("Failed to parse rule_validation_card JSON",e)}}var s=C(t,"critical_gaps");console.log("isCriticalGap",s),$e(!!s);var u="string"==typeof t?x(t):t;console.log("parsed data from Validation Agent",u);var d=E(t);Je(u);var c=!!(null==u?void 0:u.is_variant),_=void 0===(null==u?void 0:u.close_modal)||!!(null==u?void 0:u.close_modal),v=void 0!==(null==u?void 0:u.is_save)&&!!(null==u?void 0:u.is_save);Di(_),Pi(v);var p=null==u?void 0:u.action,f="string"==typeof(null==u?void 0:u.edit_variant_id)&&u.edit_variant_id.trim()?u.edit_variant_id:"string"==typeof(null==u?void 0:u.set_variant_id)&&u.set_variant_id.trim()?u.set_variant_id:null,g="variant"===p&&c;if((null==u?void 0:u.edit_variant_id)&&!g&&Xi(u.edit_variant_id),"edit"!==p&&"edit_current_rule"!==p||!f||tr(f),g&&(Xi(null),tr(null)),"discard"===p)return console.log("Action 'discard' received: Resetting UI state but preserving chat."),yn.current=!0,Tr(!0),gt([]),mi.current=null,void(wi.current=null);Qi(p);var m=null==u?void 0:u.new_variant_id,w="edit"===(null==u?void 0:u.action)&&(null==u?void 0:u.set_variant_id)&&!ie;(null==u?void 0:u.is_valid)?(Ni({}),Vt(function(t){var i,r,a,o,l,s,d,c,_,v,p,g;if(console.log("⏮️ PREVIOUS Staged Response:",t),!t)return console.warn("⚠️ No previous staged response found"),t;var m=function(e,n){return Object.prototype.hasOwnProperty.call(e||{},n)},w=e(e({},t),u);Array.isArray(null===(i=null==u?void 0:u.condition_result)||void 0===i?void 0:i.when_to_apply)?w.wta=u.condition_result.when_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join(" OR "):m(u,"wta")&&void 0!==(null==u?void 0:u.wta)&&(w.wta=null!==(r=null==u?void 0:u.wta)&&void 0!==r?r:""),Array.isArray(null===(a=null==u?void 0:u.condition_result)||void 0===a?void 0:a.when_not_to_apply)?w.wnta=u.condition_result.when_not_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join(" OR "):m(u,"wnta")&&void 0!==(null==u?void 0:u.wnta)&&(w.wnta=null!==(o=null==u?void 0:u.wnta)&&void 0!==o?o:""),console.log("finalData",w),console.log("checking parsed here :",u);var y,h=f;if(Array.isArray(ft)&&h&&(y=h,ft.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===y||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===y||(null==e?void 0:e.id)&&e.id===y})),"edit"!==(null==u?void 0:u.action)||!ne&&!ie)return w;console.log("inside edit flow merge");var b=ie||[],A=f,T=A?b.findIndex(function(e){return e.variantId===A||e.variant_id&&e.variant_id===A||e.id&&e.id===A}):-1;if(-1===T&&(null==u?void 0:u.set_variant_id)&&(T=b.findIndex(function(e){return e.variantId===u.set_variant_id||e.variant_id&&e.variant_id===u.set_variant_id||e.id&&e.id===u.set_variant_id})),-1===T&&A&&Array.isArray(ft)&&(T=ft.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===A||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===A||(null==e?void 0:e.id)&&e.id===A})),-1!==T){var M,I,k,S=b[T]||(null==ft?void 0:ft[T]),C=(null==S?void 0:S.tier_id)||(null==S?void 0:S.tierId)||"1",N=(null==te?void 0:te.toLowerCase().replace(/ /g,"_"))||"tier_".concat(C);if(yi.current!==T&&(Et(T),yi.current=T),Array.isArray(null===(l=u.condition_result)||void 0===l?void 0:l.when_to_apply)?M=u.condition_result.when_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join("\n"):m(u,"wta")&&void 0!==(null==u?void 0:u.wta)?M=null!==(s=null==u?void 0:u.wta)&&void 0!==s?s:"":m(t,"wta")&&void 0!==(null==t?void 0:t.wta)&&(M=null!==(d=null==t?void 0:t.wta)&&void 0!==d?d:""),Array.isArray(null===(c=u.condition_result)||void 0===c?void 0:c.when_not_to_apply)?I=u.condition_result.when_not_to_apply.filter(function(e){return e&&"none"!==e.toLowerCase()}).join("\n"):m(u,"wnta")&&void 0!==(null==u?void 0:u.wnta)?I=null!==(_=null==u?void 0:u.wnta)&&void 0!==_?_:"":m(t,"wnta")&&void 0!==(null==t?void 0:t.wnta)&&(I=null!==(v=null==t?void 0:t.wnta)&&void 0!==v?v:""),void 0!==M){var x=Cr(M);Nr(N,T,M,x,ut,dt,null==u?void 0:u.conflict_view),hr(N,T,"wta")}if(void 0!==I){var E=Cr(I);Nr(N,T,I,E,_t,vt,null==u?void 0:u.conflict_view),hr(N,T,"wnta")}if(m(u,"rule_description")&&void 0!==(null==u?void 0:u.rule_description)?k=null!==(p=null==u?void 0:u.rule_description)&&void 0!==p?p:"":m(t,"rule_description")&&void 0!==(null==t?void 0:t.rule_description)&&(k=null!==(g=null==t?void 0:t.rule_description)&&void 0!==g?g:""),void 0!==k){var O=Cr(k);lt(function(e){var n=new Map(e),t=new Map(n.get(N)||new Map);return t.set(T,O),n.set(N,t),n}),hr(N,T,"ruleDescription")}var L=void 0;m(u,"scheduling_config")&&void 0!==(null==u?void 0:u.scheduling_config)?L=null==u?void 0:u.scheduling_config:m(t,"scheduling_config")&&void 0!==(null==t?void 0:t.scheduling_config)&&(L=null==t?void 0:t.scheduling_config),L&&(Kn(function(e){var n,i,r,a,o,l,s=new Map(e),u=new Map(s.get(N)||new Map);return u.set(T,{order_rules:{enabled:!!(null===(n=L.order_rules)||void 0===n?void 0:n.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(i=L.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)},advertiser_rules:{enabled:!!(null===(r=L.advertiser_rules)||void 0===r?void 0:r.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(a=L.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)},inventory_rules:{respect_inventory_code:!!(null===(o=L.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)},break_code_rules:{respect_break_code:!!(null===(l=L.break_code_rules)||void 0===l?void 0:l.respect_break_code)},is_default_enabled:"Default"===(null==t?void 0:t.variant_type)||"default_variant"===(null==t?void 0:t.variant_type)||"default"===(null==t?void 0:t.variant_type)}),s.set(N,u),s}),hr(N,T,"order_enabled"),hr(N,T,"advertiser_enabled"),hr(N,T,"respect_inventory_code"),hr(N,T,"respect_break_code")),gt(function(t){if(!Array.isArray(t)||T<0||!t[T])return t;var i=n([],t,!0),r=e({},i[T]);return void 0!==M&&(r.wta=M),void 0!==I&&(r.wnta=I),void 0!==k&&(r.rule_description=Cr(k)),L&&(r.scheduling_config=L),console.log("updated variantxxx",r),i[T]=r,i});var R=null==t?void 0:t.variant_type;R&&gt(function(t){var i=Array.isArray(t)?n([],t,!0):[],r=i.findIndex(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===A||(null==e?void 0:e.variant_id)&&e.variant_id===(null==u?void 0:u.set_variant_id)||(null==e?void 0:e.variantId)===A});return-1!==r?i[r]=e(e({},i[r]),{variant_type:R}):i[T]=e(e({},i[T]||{}),{variant_type:R,variant_id:A||(null==u?void 0:u.set_variant_id)}),i})}})):console.warn("⚠️ parsed.is_valid is false, not committing to structuredAIResponse"),w||(c?(It(!0),m&&m!==wi.current?(console.log("🚀 New Variant detected (".concat(m,"). Triggering loader.")),ki(!0),Hi("Loading a variant"),console.log("reaching here in new variant flow"),Ki("This may take a moment as we update the rule with the new variant."),wi.current=m):m||console.log("ℹ️ Variant flow detected without specific ID.")):(It(!1),wi.current=null),d&&Ft(d))},"Rule comparison results":function(n){var t=function(n){if("string"!=typeof n)return n;try{var t=Array.from(n.matchAll(/<!--\s*```json\s*([\s\S]*?)\s*```\s*-->/g));if(0===t.length)return JSON.parse(n);var i=t.map(function(e){try{return JSON.parse(e[1].trim())}catch(e){return console.error("Failed to parse JSON block:",e),null}}).filter(function(e){return null!==e});return 1===i.length?i[0]:i.length>1?i.reduce(function(n,t){return e(e({},n),t)},{}):null}catch(e){return console.error("Error extracting JSON:",e),null}}(n);console.log(" LLM Push Comparison Received:",t);var i=t[0].current_rule_details;i&&rn({title:i.rule_title||"",description:i.description||"",scope:i.scope||"",wta:Array.isArray(i.wta)?i.wta.join("\n"):i.wta||"",wnta:Array.isArray(i.wnta)?i.wnta.join("\n"):i.wnta||""});var r=t.map(function(e){var n,t,i=e.variant_rule_details||{},r=e.rule_difference_comparison||{},a=i.when_to_apply||i.wta||"",o=i.when_not_to_apply||i.wnta||"";return{id:(null==i?void 0:i.variant_id)&&i.variant_id||(null==i?void 0:i.id)&&i.id||"fallback-id-".concat(Math.random()),title:i.variant_title||i.rule_title||"",description:i.variant_text||i.description||"",scope:i.scope||"",wta:Array.isArray(a)?a.join("\n"):a,wnta:Array.isArray(o)?o.join("\n"):o,similarity:(null===(n=r.summary)||void 0===n?void 0:n.overall_score)||0,summaryText:(null===(t=r.summary)||void 0===t?void 0:t.overall_summary)||"",analysis:{rule_difference_comparison:r},isLoading:!1}});ln(r)}},Sr=function(e){var n=Array.isArray(e)?e.join(" "):String(e||""),t=null==n?void 0:n.match(/<ai_reasoning>([\s\S]*?)<\/ai_reasoning>/);return t?t[1]:null},Cr=function(e){return"string"!=typeof e?"":e.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,"").trim()},Nr=function(t,i,r,a,o,l,s){var u;if(void 0!==r){var d=(o.get(t)||new Map).get(i)||[],c=d.map(function(e){return e.text}).join("\n"),_=(null===(u=d[(null==d?void 0:d.length)-1])||void 0===u?void 0:u.reasoning)||"",v=Sr(r)||"";if(""===r&&d.length>0||a.trim()!==c.trim()||v!==_){var p=""===r?{updatedItems:[],newIds:[]}:function(t,i,r){if(!t.trim())return{updatedItems:n([],i,!0),newIds:[]};var a=t.match(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g)||[],o=t.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,"").replace(/<!--```json[\s\S]*?```-->/g,""),l=[],s=o.replace(/<conflicting>[\s\S]*?<\/conflicting>/g,function(e){return l.push(e),"__CONFLICT_BLOCK_".concat((null==l?void 0:l.length)-1,"__")}).split(/\s*(?:\r?\n|\\+n)\s*/).map(function(e){return e.trim().replace(/^-\s*/,"")}).filter(Boolean),u=new Map(i.map(function(e){return[e.text,e]})),d=[],c=[],_=Array.isArray(null==r?void 0:r.wta)&&t.includes("wta")?r.wta:Array.isArray(null==r?void 0:r.wnta)?r.wnta:[];return s.forEach(function(n,t){var i=n;i.includes("__CONFLICT_BLOCK_")&&(i=i.replace(/__CONFLICT_BLOCK_(\d+)__/g,function(e,n){return l[parseInt(n)]}));var r=a[t]||"",o=i.replace(/<conflicting>[\s\S]*?<\/conflicting>/g,"").trim(),s=null==_?void 0:_.find(function(e){return e.value===o}),v=u.get(i);if(v)c.push(e(e({},v),{reasoning:r||v.reasoning,conflict:s||v.conflict}));else{var p="new-".concat(Date.now(),"-").concat(t);d.push(p),c.push({id:p,text:i,reasoning:r,conflict:s})}}),{updatedItems:c,newIds:d}}(r,d,s),f=p.updatedItems,g=p.newIds;l(function(e){var n=new Map(e),r=new Map(n.get(t)||new Map);return r.set(i,f),n.set(t,r),n}),(null==g?void 0:g.length)>0&&($t(function(e){return new Set(n(n([],Array.from(e),!0),g,!0))}),setTimeout(function(){$t(function(e){var n=new Set(e);return g.forEach(function(e){return n.delete(e)}),n})},1500))}}},xr=function(e){return(Array.isArray(e)?e:e?[e]:[]).map(function(e){return String(e).replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/,"").trim().replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()})})};d(function(){Lt&&!De.current&&"save this learning"!==Lt.toLowerCase()&&(xe(!1),Se(""),yi.current=null!=xt?xt:-1)},[Lt]),d(function(){var t,i,r,a,o,l,s,u,d;if(Pn.current=oi,Array.isArray(ft)&&ft.length>0){var c=ui||"local-".concat(Date.now()),_=["ruleTitle","ruleDescription","whenToApply","whenNotToApply","scopeData","configData"],v={},p=mi.current||[];ft.forEach(function(t,i){var r,a;if(t){t.is_readOnly||(en.current=i);var o=i;if((null==t?void 0:t.variant_id)&&ie)-1!==(l=ie.findIndex(function(e){return e.variantId===t.variant_id}))&&(o=l);else if($i&&ie&&1===ft.length){var l;-1!==(l=ie.findIndex(function(e){return e.variantId===$i}))&&(o=l)}var s=null!==(r=p[o])&&void 0!==r?r:p[i],u=null!==(a=t.scope)&&void 0!==a?a:null==s?void 0:s.scope,d=xr(u).map(function(e){return e.toLowerCase().replace(/ /g,"_")}),c=d.length>0?d:wn.current.length>0?wn.current:["tier_1"];c.length>0&&(wn.current=c),console.log("prevTierRef.current",wn.current,"targetTiers",c);var f=Cr(t.title);f&&f!==Cr(null==s?void 0:s.title)&&!t.is_readOnly&&function(e,n,t,i,r,a){var o=Cr(i);o&&o!==r&&(hr(e,n,t),a(o))}("global",0,"ruleTitle",t.title,et,nt),c.forEach(function(r){var a,l,s,u,d,c=null!==(a=p[o])&&void 0!==a?a:p[i],f=!c,g=void 0!==t.rule_description&&null!==t.rule_description,m=Cr(t.rule_description),w=(null===(l=ot.get(r))||void 0===l?void 0:l.get(o))||"",y=Cr(w);g&&(m!==y||""===m&&""!==w)&&(lt(function(e){var n=new Map(e),i=new Map(n.get(r)||new Map);return i.set(o,t.rule_description),n.set(r,i),n}),hr(r,o,"ruleDescription"));var h=!1===(null==jt?void 0:jt.is_valid),b=Cr(t.wta),A=Cr(null==c?void 0:c.wta),T=(null===(s=ut.get(r))||void 0===s?void 0:s.get(o))||[];!(h&&""===b&&T.length>0)&&void 0!==t.wta&&null!==t.wta&&(b!==A||""===b&&T.length>0||0===T.length&&""!==b)&&(Nr(r,o,t.wta,b,ut,dt,null==jt?void 0:jt.conflict_view),hr(r,o,"wta"));var M=Cr(t.wnta),I=Cr(null==c?void 0:c.wnta),k=(null===(u=_t.get(r))||void 0===u?void 0:u.get(o))||[];if(!(h&&""===M&&k.length>0)&&void 0!==t.wnta&&null!==t.wnta&&(M!==I||""===M&&k.length>0||0===k.length&&""!==M)&&(Nr(r,o,t.wnta,M,_t,vt,null==jt?void 0:jt.conflict_view),hr(r,o,"wnta")),t.scheduling_config||f||t.variant_type||t.variantType){var S=t.scheduling_config||{order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1}},C=null==c?void 0:c.scheduling_config,N=null===(d=Wn.get(r))||void 0===d?void 0:d.get(o),x=t.variant_type||t.variantType,E=x?U(x):void 0,O=JSON.stringify(S)!==JSON.stringify(C),L=void 0!==E&&E!==(null==N?void 0:N.is_default_enabled),R=function(n,t){if(!n)return t;if(!t)return n;var i=e({},n);for(var r in t)t[r]&&"object"==typeof t[r]&&!Array.isArray(t[r])?i[r]=e(e({},i[r]||{}),t[r]):i[r]=t[r];return i};if(Wt(function(e){return R(e,S)}),O||L){Wt(function(e){return R(e,S)});var D=function(e,n,t,i){var r,a,o,l,s=[],u=(i.get(e)||new Map).get(n)||{order_rules:{enabled:!1,global_min_separation_seconds:0},advertiser_rules:{enabled:!1,global_min_separation_seconds:0},inventory_rules:{respect_inventory_code:!1},break_code_rules:{respect_break_code:!1}};return Math.floor(((null===(r=t.order_rules)||void 0===r?void 0:r.global_min_separation_seconds)||0)/60)!==u.order_rules.global_min_separation_seconds&&s.push("config_order_rules"),Math.floor(((null===(a=t.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)!==u.advertiser_rules.global_min_separation_seconds&&s.push("config_advertiser_rules"),!!(null===(o=t.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)!==u.inventory_rules.respect_inventory_code&&s.push("config_inventory_rules"),!!(null===(l=t.break_code_rules)||void 0===l?void 0:l.respect_break_code)!==u.break_code_rules.respect_break_code&&s.push("config_break_code_rules"),!!t.is_default_enabled!=!!u.is_default_enabled&&s.push("config_is_default_enabled"),s}(r,o,S,Wn);D.forEach(function(e){return hr(r,o,e)}),Qn(function(e){return new Set(e).add("config")}),Kn(function(n){var t,i,a,l,s,u,d=new Map(n),c=new Map(d.get(r)||new Map),_=c.get(o)||Hn;return c.set(o,e(e(e(e(e(e({},_),S.order_rules&&{order_rules:{enabled:!!(null===(t=S.order_rules)||void 0===t?void 0:t.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(i=S.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)}}),S.advertiser_rules&&{advertiser_rules:{enabled:!!(null===(a=S.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds),global_min_separation_seconds:Math.floor(((null===(l=S.advertiser_rules)||void 0===l?void 0:l.global_min_separation_seconds)||0)/60)}}),S.inventory_rules&&{inventory_rules:{respect_inventory_code:!!(null===(s=S.inventory_rules)||void 0===s?void 0:s.respect_inventory_code)}}),S.break_code_rules&&{break_code_rules:{respect_break_code:!!(null===(u=S.break_code_rules)||void 0===u?void 0:u.respect_break_code)}}),void 0!==E&&{is_default_enabled:E})),d.set(r,c),d}),rt(function(e){var n=new Map(e),t=new Map(n.get(r)||new Map),i=t.get(o)||new Set;return S.order_rules&&i.add("order_rules"),S.advertiser_rules&&i.add("advertiser_rules"),S.inventory_rules&&i.add("inventory_rules"),S.break_code_rules&&i.add("break_code_rules"),void 0!==E&&i.add("is_default_enabled"),t.set(o,i),n.set(r,t),n})}}var j={ruleTitle:t.title,ruleDescription:t.rule_description,whenToApply:t.wta,whenNotToApply:t.wnta,scopeData:t.scope,configData:t.action};t.is_readOnly||_.forEach(function(e){var n=Sr(j[e]),t="ruleTitle"===e||"scopeData"===e?e:"".concat(e,"_").concat(r);n&&(n!==Vn.current[t]&&(Vn.current[t]=n,Fn.current[t]=!0,v[t]={reasoning:n,associatedCitations:[],tier:r,variantIndex:o}))}),t.conflictingConditions&&(Ni(function(n){var i=e({},n);return t.conflictingConditions.forEach(function(e){e.condition_wta&&(i["whenToApply-".concat(r,"-").concat(o)]=e.reason),e.condition_wnta&&(i["whenNotToApply-".concat(r,"-").concat(o)]=e.reason)}),i}),Qn(function(e){return new Set(n(n([],Array.from(e),!0),["conditions"],!1))}))})}});var f=-1,g=!(!ne&&!ie),m="edit"===zi||"edit_current_rule"===zi,w=function(e,n){return(null==e?void 0:e.variant_id)&&e.variant_id===n||(null==e?void 0:e.new_variant_id)&&e.new_variant_id===n||(null==e?void 0:e.id)&&e.id===n},y=function(e,n){if(!n)return-1;var t=e.find(function(e){return w(e,n)});if(!t)return-1;var i=String((null==t?void 0:t.tier_id)||"");if(!i)return e.findIndex(function(e){return w(e,n)});var r=e.filter(function(e){return String((null==e?void 0:e.tier_id)||"")===i}).findIndex(function(e){return w(e,n)});return-1!==r?r:e.findIndex(function(e){return w(e,n)})};if(g)if(wi.current&&Mt)-1!==(f=ft.findIndex(function(e){return(null==e?void 0:e.new_variant_id)&&e.new_variant_id===wi.current||(null==e?void 0:e.variant_id)&&e.variant_id===wi.current||(null==e?void 0:e.id)&&e.id===wi.current}))&&(wi.current=null);else if($i||nr){var h;(h=$i||nr)&&(f=y(ft,h))}else if(oe&&Array.isArray(ft)&&ft.length>0){for(b=ft.length-1;b>=0;b--)if(ft[b]&&!(null===(i=ft[b])||void 0===i?void 0:i.is_readOnly)){f=b;break}}else null==re||mi.current&&0!==mi.current.length||(f=re);else if((h=$i||nr||wi.current)&&m&&(f=y(ft,h)),!m&&-1===f&&Array.isArray(ft)&&ft.length>0){for(var b=ft.length-1;b>=0;b--)if(ft[b]&&!(null===(t=ft[b])||void 0===t?void 0:t.is_readOnly)){f=b;break}-1===f&&(f=ft.length-1)}-1!==f&&xt!==f&&(yi.current===f||We.current&&!m||(console.log("🎯 Setting active variant accordion to index ".concat(f," (").concat(g?"EDIT":"CREATE"," mode)")),Et(f),yi.current=f));var A=ft[0];if(A){if(A.scope&&JSON.stringify(A.scope)!==JSON.stringify(null===(r=p[0])||void 0===r?void 0:r.scope)&&"copy"!==A.action&&(l=A.scope,s=null===(a=p[0])||void 0===a?void 0:a.scope,u=xr(l),d=s?xr(s):[],ge(function(n){return e(e({},n),{tiers:u})}),u.forEach(function(e){var n=e.trim();d.includes(e)||hr("global",0,"scope_".concat(n))})),A.scope_rule_set&&A.scope_rule_set!==(null===(o=p[0])||void 0===o?void 0:o.scope_rule_set)){var T=A.scope_rule_set.toLowerCase().trim();["global","genre","custom"].includes(T)&&(ge(function(n){return e(e({},n),{station:T})}),hr("global",0,"scope_station"))}A.wtl&&$n(A.wtl)}mi.current=ft.map(function(e){return e?(n=e,"function"==typeof(t=null===globalThis||void 0===globalThis?void 0:globalThis.structuredClone)?t(n):JSON.parse(JSON.stringify(n))):e;var n,t}),Object.keys(v).length>0&&(sr({type:"SAVE_CITATIONS",requestId:c,reasoningMap:v,rawCitations:oi}),function(e){var n=e.requestId,t=e.keys,i=e.newReasoningMap,r=e.getLatestReasoning,a=e.currentCitations;console.group("%c Citation Audit: ".concat(n," "),"background: #5928ed; color: #fff; font-weight: bold;");var o={};t.forEach(function(e){var n=r(e),t=!!i[e],l=t?a:(null==n?void 0:n.associatedCitations)||[];o[e]={Status:t?"NEW ✅":n?"FALLBACK 🔄":"EMPTY ❌",Source:t?"Current Msg":n?"History":"N/A","Reasoning Preview":t?i[e].reasoning:(null==n?void 0:n.reasoning)||"None","Full Citations":l}}),console.table(o),console.groupEnd()}({requestId:c,keys:Object.keys(v),newReasoningMap:v,getLatestReasoning:ur,currentCitations:oi}),ri(function(n){var t=e({},n),i=!1;return Object.keys(v).forEach(function(e){var n=v[e];n.reasoning&&t[e]!==n.reasoning&&(t[e]=n.reasoning,i=!0),n.associatedCitations}),i?t:n}))}},[ft,oi,ui]),d(function(){var t=Object.keys(Fn.current);if(0!==t.length){var i={};t.forEach(function(e){i[e]=n([],oi,!0)}),Fn.current={},vi(function(n){return e(e({},n),i)})}},[oi]);var Er=function(e){return e.map(function(e){var n;return null===(n=e.match(/\d+/))||void 0===n||n[0],e.replace(/\s+/g,"-"),{name:"Custom",parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:(null==Un?void 0:Un.CUSTOM_SCOPE_GROUP_NAME)||"",description:(null==ee?void 0:ee.ruleSetDesc)||""}}})},Or=function(e){var n={};return Object.entries(e).forEach(function(e){var t,i=e[0],r=e[1];if("custom_sep_time_for_entities"===i&&r)n.custom_sep_time_for_entities=r;else if("max_priority_override"===i&&r&&void 0!==r.override_priority_code)n.override_priority_code=r.override_priority_code;else if("advertiser_pair_break_constraint"===i&&r)n.advertiser_pair_break_constraint=r.advertiser_pair_list;else if(r&&"object"==typeof r&&(null===(t=Object.keys(r))||void 0===t?void 0:t.length)>0){var a=i.replace(/_rules$/,""),o=["global_min_separation_seconds","respect_inventory_code","respect_break_code","exemption_advertiser_list","exemption_break_code_list","exemption_priority_code_list","priority_code_list","exemption_product_code_list","condition_advertiser_list","condition_break_code_list","condition_priority_code_list","condition_product_code_list"];Object.entries(r).forEach(function(e){var t=e[0],r=e[1];if(o.includes(t)&&("product_rules"!==i||"global_min_separation_seconds"!==t)){var l="respect_inventory_code"===t||"respect_break_code"===t?t:"".concat(a,"_").concat(t);n[l]=r}})}else if(void 0!==r&&"object"!=typeof r){if("station_guid"===i||"is_default_enabled"===i)return;n[i]=r}}),{config_structure:{scheduling_config:{advertiser_exemption_advertiser_list:"array",advertiser_exemption_break_code_list:"array",advertiser_exemption_priority_code_list:"array",advertiser_exemption_product_code_list:"array",advertiser_global_min_separation_seconds:"int",break_code_exemption_advertiser_list:"array",break_code_exemption_break_code_list:"array",break_code_exemption_priority_code_list:"array",break_code_exemption_product_code_list:"array",custom_sep_time_for_entities:"object",inventory_exemption_advertiser_list:"array",inventory_exemption_break_code_list:"array",inventory_exemption_priority_code_list:"array",inventory_exemption_product_code_list:"array",order_exemption_advertiser_list:"array",order_exemption_break_code_list:"array",order_exemption_priority_code_list:"array",order_exemption_product_code_list:"array",order_global_min_separation_seconds:"int",override_priority_code:"str",product_exemption_advertiser_list:"array",product_exemption_break_code_list:"array",product_exemption_priority_code_list:"array",product_exemption_product_code_list:"array",max_priority_override_priority_code_list:"array",max_priority_override_exemption_advertiser_list:"array",max_priority_override_exemption_break_code_list:"array",max_priority_override_exemption_priority_code_list:"array",max_priority_override_exemption_product_code_list:"array",first_in_break_exemption_advertiser_list:"array",first_in_break_exemption_break_code_list:"array",first_in_break_exemption_priority_code_list:"array",first_in_break_exemption_product_code_list:"array",first_in_break_condition_advertiser_list:"array",first_in_break_condition_break_code_list:"array",first_in_break_condition_priority_code_list:"array",first_in_break_condition_product_code_list:"array",advertiser_pair_break_constraint:"array",respect_break_code:"bool",respect_inventory_code:"bool"}},config_data:{scheduling_config:n}}},Lr=function(n,t,i,r){void 0===r&&(r=0);var a=e({},t||{});return n.forEach(function(n,o){var l=n.get(r);l&&l.forEach(function(n){var l,s=t[n]||{},u=i.get(o),d=null==u?void 0:u.get(r),c=null==d?void 0:d[n];if("order_rules"===n||"advertiser_rules"===n){var _=null!==(l=null==c?void 0:c.enabled)&&void 0!==l&&l?60*((null==c?void 0:c.global_min_separation_seconds)||0):0;a[n]=e(e({},s),{global_min_separation_seconds:_})}else a[n]="inventory_rules"===n?e(e({},s),{respect_inventory_code:null==c?void 0:c.respect_inventory_code}):"break_code_rules"===n?e(e({},s),{respect_break_code:null==c?void 0:c.respect_break_code}):"is_default_enabled"===n?null!=c&&c:s})}),a},Rr=function(e,n,t){void 0===t&&(t="variantType");var i=null==e?void 0:e[n];if(!i)return{label:Mr(e),isDefault:!1};var r=i[t]||"",a=U(r);return{label:"variant ".concat(n+1),isDefault:a}},Dr=function(e,n){var t=U(e);return{label:"variant ".concat(n+1),isDefault:t}},jr=function(e,n,t){var i;void 0===t&&(t="");var r=null===(i=Wn.get(e))||void 0===i?void 0:i.get(n);return r?r.is_default_enabled?"Default":"Variant":U(t)?"Default":"Variant"},Vr=function(n,o){return void 0===o&&(o=!1),t(void 0,void 0,void 0,function(){var t,l,s,u,d,c,_,p,g,w,h,b,A,T,k,S,C;return i(this,function(i){switch(i.label){case 0:t=Er(fe.tiers),l=M.getState().backwardCompatibility,s=en.current,u=Lr(it,Bt,Wn,s),i.label=1;case 1:return i.trys.push([1,3,,4]),ye(F),Se(""),d=Or(u),c=m(),_=[],p=[],g=(ft||[])[s]||{},console.log("originalItem check :",g,s),fe.tiers.forEach(function(i){var r,a,o,u,v,f,w,h,b,A,T,M,I,k,S,C=i.toLowerCase().replace(/ /g,"_");p.push(C);var N=(null===(r=ot.get(C))||void 0===r?void 0:r.get(s))||"",x=(null===(a=ut.get(C))||void 0===a?void 0:a.get(s))||[],E=(null===(o=_t.get(C))||void 0===o?void 0:o.get(s))||[];if(!1===l){var O=[e(e({},d&&{config:e({master_name:Un.CONFIG_MASTER_NAME},d)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},tierLinkId:c,variant:Cr(N),conditions_whentoapply:x.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:E.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null===(f=null===(v=null===(u=Wn.get(C))||void 0===u?void 0:u.get(s))||void 0===v?void 0:v.is_default_enabled)||void 0===f||f,tier_id:(null===(w=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===C||e.tier_name.toLowerCase()===C.replace(/_/g,"")}))||void 0===w?void 0:w.tier_id)||"",topic_id:null,scope_id:(null==ee?void 0:ee.ruleSetId)||"",question_title:et,variantType:jr(C,s,g.variant_type||g.variantType),instantLearningType:Un.INSTANT_LEARNING_TYPE,is_user_accepted_risk:!1,propagation:!1,applicationMappingId:ve,modeId:"019e1e39-2030-7775-8312-0ad525700588"})];_.push(y(O,Un.ASSISTANT_NAME))}else{O={status:"approved",metadata:e(e({userName:null!==(h=null==or?void 0:or.firstname)&&void 0!==h?h:"",userEmailId:null!==(b=null==or?void 0:or.email)&&void 0!==b?b:"",llm:D.MODEL,isVariantFlow:!0},d&&{config:e({master_name:D.CONFIG_MASTER_NAME},d)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:c,variant:Cr(N),conditions_whentoapply:x.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:E.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:t[0]||{name:D.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:D.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null===(M=null===(T=null===(A=Wn.get(C))||void 0===A?void 0:A.get(s))||void 0===T?void 0:T.is_default_enabled)||void 0===M||M,tier:{name:i.split(" ").join("").toLowerCase(),tierGroupName:D.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(i.split(" ")[1]||"1"),tier_id:(null===(I=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===C||e.tier_name.toLowerCase()===C.replace(/_/g,"")}))||void 0===I?void 0:I.tier_id)||""},topic:{id:null,value:"".concat(fe.station||X,"RuleSet"),description:null},categories:[{id:null,value:D.CATEGORY,description:D.CATEGORY}],originalQuestion:et,questionId:n.get(C)||null,variantType:jr(C,s,g.variant_type||g.variantType),parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(k=null==or?void 0:or.email)&&void 0!==k?k:"",instantLearningType:D.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(S=null==g?void 0:g.is_user_accepted_risk)&&void 0!==S?S:null==jt?void 0:jt.is_user_accepted_risk)},isAgentsFeedback:!0})};_.push(y(O,Un.ASSISTANT_NAME))}}),[4,Promise.all(_)];case 2:return w=i.sent(),Re(!1),console.log("✅ All feedback published:",w),ye(P),Ae(!0),h=new Map,w.forEach(function(e,n){var t,i,r=p[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;if(a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data),a&&"string"==typeof a){var o=h.get(r)||[];o.push({id:a,action:"new"}),h.set(r,o)}}),M.getState().setCreatedVariantInfo(Array.from(h.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),null===(k=hi.current)||void 0===k||k.show({severity:"success",sticky:!1,life:6e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Learning saved successfully!"}))]}))}),Ee.current=setTimeout(function(){Ri||o?(console.log("Closing modal after save success",{isCloseModal:Ri,forceClose:o}),ra()):Tr(!0)},3e3),[3,4];case 3:return b=i.sent(),console.error("❌ Error publishing feedback:",b),Re(!1),ye(G),xe(!0),A=(null===(S=null==b?void 0:b.response)||void 0===S?void 0:S.data)||{},console.log("errorData: ",A),(T="Variant already exists"===(null==A?void 0:A.message))||409===(null==b?void 0:b.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==A?void 0:A.message)||(null==A?void 0:A.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(C=hi.current)||void 0===C||C.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(v,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:T||409===(null==b?void 0:b.status)?"Similar rule already exists. Please update and try again.":(null==A?void 0:A.message)||(null==A?void 0:A.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,4];case 4:return[2]}})})},Pr=function(){return t(void 0,void 0,void 0,function(){var t,o,l,s,u,d,c,_,p,g,w,b,A,T,k,S,C,N,x,E,O,L,R,D,j,V;return i(this,function(i){switch(i.label){case 0:t=M.getState().tiers,o=Lr(it,Bt,Wn),l=Or(o),s=en.current,u=(ft||[])[s]||{},d=(null==jt?void 0:jt.variant_type)||u.variant_type||"",i.label=1;case 1:for(i.trys.push([1,3,,4]),ye(F),Se(""),_=(c=ie||[]).length>0?null===(O=c.find(function(e){return(null==e?void 0:e.link_id)&&""!==e.link_id.trim()}))||void 0===O?void 0:O.link_id:null,p=(null===(L=fe.tiers[0])||void 0===L?void 0:L.toLowerCase().replace(/ /g,"_"))||"tier_1",g=(null===(R=ot.get(p))||void 0===R?void 0:R.size)||c.length,w=n([],c,!0),b=c.length;b<g;b++)w.push({isNewVariant:!0,variantId:m()});return A=[],T=[],k=Er(fe.tiers),fe.tiers.forEach(function(n){var i=n.toLowerCase().replace(/ /g,"_"),r=t.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===i||e.tier_name.toLowerCase()===i.replace(/_/g,"")}),a=ot.get(i),o=ut.get(i),s=_t.get(i);w.forEach(function(t,c){var v,p,f,g,w,b,M,I,S,C,N,x,E,O,L,R,D,j,V,P,G,F,J,H,B,W,K,Y,z=(null==a?void 0:a.get(c))||t.description||"",Q=(null==o?void 0:o.get(c))||[],Z=(null==s?void 0:s.get(c))||[];if(t.isNewVariant){T.push({tierKey:i,payloadType:"new"});var $=Lr(it,Bt,Wn,c),X=Or($);console.log("variant in save edit add part :",t,u);var te={status:"approved",metadata:e(e({userName:null!==(v=null==or?void 0:or.firstname)&&void 0!==v?v:"",userEmailId:null!==(p=null==or?void 0:or.email)&&void 0!==p?p:"",llm:Un.MODEL,isVariantFlow:!0},X&&{config:e({master_name:Un.CONFIG_MASTER_NAME},X)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:_||m(),variant:Cr(z),conditions_whentoapply:Q.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:Z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:k[0]||{name:Un.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:Un.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null!==(w=null===(g=null===(f=Wn.get(i))||void 0===f?void 0:f.get(c))||void 0===g?void 0:g.is_default_enabled)&&void 0!==w?w:U(t.variantType||t.variant_type||(0===c?"Default":"variant")),tier:{name:n.split(" ").join("").toLowerCase(),tierGroupName:Un.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(n.split(" ")[1]||"1")},topic:{id:null,value:"".concat(fe.station,"RuleSet"),description:null},categories:[{id:null,value:Un.CATEGORY,description:Un.CATEGORY}],originalQuestion:et,questionId:null==ne?void 0:ne.ruleId,variantType:jr(i,c,u.variant_type||""),parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(b=null==or?void 0:or.email)&&void 0!==b?b:"",instantLearningType:Un.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(I=null===(M=(ft||[])[c])||void 0===M?void 0:M.is_user_accepted_risk)&&void 0!==I?I:null==jt?void 0:jt.is_user_accepted_risk)},isAgentsFeedback:!0})};console.log("payload (publish new variant)",te),A.push(y(te,Un.ASSISTANT_NAME))}else{T.push({tierKey:i,payloadType:"existing",existingId:t.variantId});var ie=t.variantId===$i,re=jr(i,c,ie?d||t.variantType||"":t.variantType||"");console.log("isThisVariantBeingEdited variantTypeToUse :",re,ie,t,$i);var ae={variantId:t.variantId,ruleDesc:(null==ee?void 0:ee.ruleSetDesc)||"",whenToApply:Q.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),whenNotToApply:Z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null!==(N=null===(C=null===(S=Wn.get(i))||void 0===S?void 0:S.get(c))||void 0===C?void 0:C.is_default_enabled)&&void 0!==N?N:U(t.variantType||t.variant_type||(0===c?"Default":"variant")),tier:{tier_id:(null==r?void 0:r.tier_id)||"",tier_name:(null==r?void 0:r.tier_name)||n,tier_priority:null!==(x=null==r?void 0:r.tier_priority)&&void 0!==x?x:null},scope_id:(null==ee?void 0:ee.ruleSetId)||"",description:Cr(z)||"",topic:"".concat(fe.station,"RuleSet"),configuration:(null==l?void 0:l.config_data)||{},tierLinkId:null!=_?_:null,variantType:re,is_user_accepted_risk:!!(null!==(O=null===(E=(ft||[])[c])||void 0===E?void 0:E.is_user_accepted_risk)&&void 0!==O?O:null==t?void 0:t.is_user_accepted_risk),is_enabled:t.is_enabled},oe=(wt.get(i)||[]).find(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===t.variantId}),le=!0;if(oe){var se=function(e){var n=(e||"").trim();return(n=q(n)).replace(/\s+/g," ").replace(/(\s*OR\s*)/g," OR ")},ue=se(et)===se(oe.title),de=se(z)===se(oe.rule_description),ce=se(ae.whenToApply)===se(oe.wta),_e=se(ae.whenNotToApply)===se(oe.wnta),ve=!!ae.isDefault===U(oe.variant_type),pe=oe.scheduling_config||{},ge=null!==(D=null!==(L=pe.order_global_min_separation_seconds)&&void 0!==L?L:null===(R=pe.order_rules)||void 0===R?void 0:R.global_min_separation_seconds)&&void 0!==D?D:0,me=null!==(P=null!==(j=pe.advertiser_global_min_separation_seconds)&&void 0!==j?j:null===(V=pe.advertiser_rules)||void 0===V?void 0:V.global_min_separation_seconds)&&void 0!==P?P:0,we=null!==(J=null!==(F=null===(G=pe.inventory_rules)||void 0===G?void 0:G.respect_inventory_code)&&void 0!==F?F:pe.respect_inventory_code)&&void 0!==J&&J,ye=null!==(W=null!==(B=null===(H=pe.break_code_rules)||void 0===H?void 0:H.respect_break_code)&&void 0!==B?B:pe.respect_break_code)&&void 0!==W&&W,he=(null===(K=Wn.get(i))||void 0===K?void 0:K.get(c))||Hn,be=he.order_rules.enabled===ge>0&&he.order_rules.global_min_separation_seconds===Math.floor(ge/60)&&he.advertiser_rules.enabled===me>0&&he.advertiser_rules.global_min_separation_seconds===Math.floor(me/60)&&he.inventory_rules.respect_inventory_code===we&&he.break_code_rules.respect_break_code===ye;ue&&de&&ce&&_e&&ve&&be&&(le=!1)}le?(console.log("payloadv (update existing variant)",ae),A.push(h(ae,null!==(Y=null==or?void 0:or.email)&&void 0!==Y?Y:""))):(console.log("[saveEditedVariant] No changes for variant ".concat(t.variantId,", skipping API call")),A.push(Promise.resolve({data:{id:t.variantId},skipped:!0})))}})}),[4,Promise.all(A)];case 2:return S=i.sent(),console.log("✅ All variants updated:",S),ye(P),Ae(!0),Re(!1),C=new Map,S.forEach(function(e,n){var t,i;if(!(null==e?void 0:e.skipped)){var r=T[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id);a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data);var o="existing"===r.payloadType?r.existingId:a;if(o&&"string"==typeof o){var l=C.get(r.tierKey)||[];l.push({id:o,action:r.payloadType}),C.set(r.tierKey,l)}}}),M.getState().setCreatedVariantInfo(Array.from(C.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),null===(D=hi.current)||void 0===D||D.show({severity:"success",sticky:!1,life:3e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Changes saved successfully!"}))]}))}),Ee.current=setTimeout(function(){ra()},3e3),[3,4];case 3:return N=i.sent(),console.error("❌ Error updating variant:",N),ye(G),xe(!0),Re(!1),x=(null===(j=null==N?void 0:N.response)||void 0===j?void 0:j.data)||{},console.log("errorData: ",x),(E="Variant already exists"===(null==x?void 0:x.message))||409===(null==N?void 0:N.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==x?void 0:x.message)||(null==x?void 0:x.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(V=hi.current)||void 0===V||V.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(v,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:E||409===(null==N?void 0:N.status)?"Similar rule already exists. Please update and try again.":(null==x?void 0:x.message)||(null==x?void 0:x.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,4];case 4:return[2]}})})},Gr=function(){return t(void 0,void 0,void 0,function(){var n,t,o,l,s,u,d,c,_,p,g,w,b,A,T,k,S,C,N,x,E,O,L,R,D,j,V;return i(this,function(i){switch(i.label){case 0:n=M.getState().tiers,t=Lr(it,Bt,Wn),o=Or(t),i.label=1;case 1:return i.trys.push([1,9,,10]),ye(F),Se(""),0===(l=Array.isArray(ft)?ft:[]).length?(console.warn("No variants found to save."),ye(G),[2]):(s=l.filter(function(e){return e.is_new_variant}),u=l.filter(function(e){return!e.is_new_variant&&(null==e?void 0:e.variant_id)}),console.log("[saveLoadAndEditVariant] New: ".concat(s.length,", Existing: ").concat(u.length)),d=u.length>0?null===(R=u.find(function(e){return(null==e?void 0:e.link_id)&&""!==e.link_id.trim()}))||void 0===R?void 0:R.link_id:null,c=[],_=[],p=[],g=[],w=Er(fe.tiers),fe.tiers.forEach(function(t){var i=t.toLowerCase().replace(/ /g,"_"),r=n.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===i||e.tier_name.toLowerCase()===i.replace(/_/g,"")}),a=ot.get(i),s=ut.get(i),u=_t.get(i);l.forEach(function(n,l){var v,f,y,b,A,T,M,I,k,S,C,N,x,E,O,L,R,D,j,V,P,G,F,J,H,B,W,K=(null==a?void 0:a.get(l))||n.rule_description||n.description||"",Y=(null==s?void 0:s.get(l))||[],z=(null==u?void 0:u.get(l))||[];if(n.is_new_variant){var Q=Lr(it,Bt,Wn,l),Z=Or(Q),$=jr(i,l,n.variant_type||n.variantType||""),ne={status:"approved",metadata:e(e({userName:null!==(v=null==or?void 0:or.firstname)&&void 0!==v?v:"",userEmailId:null!==(f=null==or?void 0:or.email)&&void 0!==f?f:"",llm:Un.MODEL,isVariantFlow:!0},Z&&{config:e({master_name:Un.CONFIG_MASTER_NAME},Z)}),{datasource:{type:"link",ref:"www.something.com",tag:"something"},variant:{tierLinkId:d||m(),variant:Cr(K),conditions_whentoapply:Y.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),conditions_whennottoapply:z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),referenceVariantID:null,referenceVariantIDVersion:null,scope:w[0]||{name:Un.SCOPE_NAME,parentScopeId:null,scopeRowId:(null==ee?void 0:ee.ruleSetId)||"",value:{scopeName:(null==ee?void 0:ee.ruleSetName)||"",scopeId:"Audacy_"+m(),customScopeGroupName:Un.CUSTOM_SCOPE_GROUP_NAME,description:(null==ee?void 0:ee.ruleSetDesc)||""}},overriden:!1,isDefault:null!==(A=null===(b=null===(y=Wn.get(i))||void 0===y?void 0:y.get(l))||void 0===b?void 0:b.is_default_enabled)&&void 0!==A?A:U(n.variantType||n.variant_type||(0===l?"Default":"variant")),tier:{name:t.split(" ").join("").toLowerCase(),tierGroupName:Un.CUSTOM_SCOPE_GROUP_NAME,tierPriority:parseInt(t.split(" ")[1]||"1"),tier_id:(null==r?void 0:r.tier_id)||""},topic:{id:null,value:"".concat(fe.station||X,"RuleSet"),description:null},categories:[{id:null,value:Un.CATEGORY,description:Un.CATEGORY}],originalQuestion:et,questionId:Rn.current||null,variantType:$,parameterizedQuestion:null,created_at:(new Date).toISOString(),created_by:null!==(T=null==or?void 0:or.email)&&void 0!==T?T:"",instantLearningType:Un.INSTANT_LEARNING_TYPE,appliedTagsIL:[],tagMappingInfoIL:{},is_user_accepted_risk:!!(null!==(M=null==n?void 0:n.is_user_accepted_risk)&&void 0!==M?M:null==jt?void 0:jt.is_user_accepted_risk)},isAgentsFeedback:!0})};console.log("[saveLoadAndEditVariant] publish payload (new variant)",ne),_.push({payload:ne,tierKey:i}),g.push({tierKey:i})}else if(null==n?void 0:n.variant_id){p.push({tierKey:i,existingId:n.variant_id});var te=(null==n?void 0:n.variant_id)&&n.variant_id===rr?(null==jt?void 0:jt.variant_type)||n.variant_type||n.variantType||"":n.variant_type||n.variantType||"",ie=($=jr(i,l,te),{variantId:n.variant_id,ruleDesc:(null==ee?void 0:ee.ruleSetDesc)||"",whenToApply:Y.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),whenNotToApply:z.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" OR "),isDefault:null!==(S=null===(k=null===(I=Wn.get(i))||void 0===I?void 0:I.get(l))||void 0===k?void 0:k.is_default_enabled)&&void 0!==S?S:U(n.variantType||n.variant_type||(0===l?"Default":"variant")),tier:{tier_id:(null==r?void 0:r.tier_id)||"",tier_name:(null==r?void 0:r.tier_name)||t,tier_priority:null!==(C=null==r?void 0:r.tier_priority)&&void 0!==C?C:null},scope_id:(null==ee?void 0:ee.ruleSetId)||"",description:Cr(K)||"",topic:"".concat(fe.station,"RuleSet"),configuration:(null==o?void 0:o.config_data)||{},tierLinkId:null!=d?d:null,variantType:$,is_user_accepted_risk:!!(null!==(N=null==n?void 0:n.is_user_accepted_risk)&&void 0!==N?N:null===(x=(ft||[])[l])||void 0===x?void 0:x.is_user_accepted_risk),is_enabled:n.is_enabled}),re=(wt.get(i)||[]).find(function(e){return(null==e?void 0:e.variant_id)&&e.variant_id===(null==n?void 0:n.variant_id)}),ae=!0;if(re){var oe=function(e){var n=(e||"").trim();return(n=q(n)).replace(/\s+/g," ").replace(/(\s*OR\s*)/g," OR ")},le=oe(et)===oe(re.title),se=oe(K)===oe(re.rule_description),ue=oe(ie.whenToApply)===oe(re.wta),de=oe(ie.whenNotToApply)===oe(re.wnta),ce=!!ie.isDefault===U(re.variant_type),_e=re.scheduling_config||{},ve=null!==(L=null!==(E=_e.order_global_min_separation_seconds)&&void 0!==E?E:null===(O=_e.order_rules)||void 0===O?void 0:O.global_min_separation_seconds)&&void 0!==L?L:0,pe=null!==(j=null!==(R=_e.advertiser_global_min_separation_seconds)&&void 0!==R?R:null===(D=_e.advertiser_rules)||void 0===D?void 0:D.global_min_separation_seconds)&&void 0!==j?j:0,ge=null!==(G=null!==(P=null===(V=_e.inventory_rules)||void 0===V?void 0:V.respect_inventory_code)&&void 0!==P?P:_e.respect_inventory_code)&&void 0!==G&&G,me=null!==(H=null!==(J=null===(F=_e.break_code_rules)||void 0===F?void 0:F.respect_break_code)&&void 0!==J?J:_e.respect_break_code)&&void 0!==H&&H,we=(null===(B=Wn.get(i))||void 0===B?void 0:B.get(l))||Hn,ye=we.order_rules.enabled===ve>0&&we.order_rules.global_min_separation_seconds===Math.floor(ve/60)&&we.advertiser_rules.enabled===pe>0&&we.advertiser_rules.global_min_separation_seconds===Math.floor(pe/60)&&we.inventory_rules.respect_inventory_code===ge&&we.break_code_rules.respect_break_code===me;le&&se&&ue&&de&&ce&&ye&&(ae=!1)}ae?(console.log("[saveLoadAndEditVariant] update payload (existing variant)",ie),c.push(h(ie,null!==(W=null==or?void 0:or.email)&&void 0!==W?W:""))):(console.log("[saveLoadAndEditVariant] No changes for variant ".concat(n.variant_id,", skipping API call")),c.push(Promise.resolve({data:{id:n.variant_id},skipped:!0})))}})}),[4,Promise.all(c)]);case 2:b=i.sent(),console.log("✅ All existing variants updated:",b),A=[],T=0,k=_,i.label=3;case 3:if(!(T<k.length))return[3,8];S=k[T],i.label=4;case 4:return i.trys.push([4,6,,7]),[4,y(S.payload,Un.ASSISTANT_NAME)];case 5:return C=i.sent(),A.push(C),[3,7];case 6:throw N=i.sent(),console.error("❌ Error publishing new variant:",N),N;case 7:return T++,[3,3];case 8:return console.log("✅ All new variants published:",A),x=new Map,b.forEach(function(e,n){var t,i;if(!(null==e?void 0:e.skipped)){var r=p[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data);var o=r.existingId||a;if(o&&"string"==typeof o){var l=x.get(r.tierKey)||[];l.push({id:o,action:"existing"}),x.set(r.tierKey,l)}}}),A.forEach(function(e,n){var t,i,r=g[n],a=(null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.id)||(null===(i=null==e?void 0:e.data)||void 0===i?void 0:i.variant_id)&&e.data.variant_id||(null==e?void 0:e.id)||(null==e?void 0:e.variant_id)&&e.variant_id;if(a||"string"!=typeof(null==e?void 0:e.data)||(a=e.data),a&&"string"==typeof a){var o=x.get(r.tierKey)||[];o.push({id:a,action:"new"}),x.set(r.tierKey,o)}}),M.getState().setCreatedVariantInfo(Array.from(x.entries()).map(function(e){return{tierName:e[0],variants:e[1]}})),ye(P),Ae(!0),Re(!1),We.current=!1,null===(D=hi.current)||void 0===D||D.show({severity:"success",sticky:!1,life:3e3,className:I.toastSuccess,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(f,{className:I.iconSuccess}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleSuccess)},{children:"Success"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageSuccess)},{children:"Changes saved successfully!"}))]}))}),Ee.current=setTimeout(function(){ra()},3e3),[3,10];case 9:return E=i.sent(),console.error("❌ Error updating variant:",E),ye(G),xe(!0),Re(!1),O=(null===(j=null==E?void 0:E.response)||void 0===j?void 0:j.data)||{},console.log("errorData: ",O),(L="Variant already exists"===(null==O?void 0:O.message))||409===(null==E?void 0:E.status)?Se('Looks like a similar rule already exists with the same conditions. You can update the description or conditions to make it unique, or discard this learning. <br/> How would you like to proceed?\n <br/><button class="clickable-button" data-value="Discard learning">Discard learning</button>\n <br/><button class="clickable-button" data-value="Continue editing learning">Continue editing learning</button>\n <br/><button class="clickable-button" id="typeYourAnswer">Or, type your response</button>'):Se(((null==O?void 0:O.message)||(null==O?void 0:O.detail)||"Error Occurred while saving the rule.")+'Please try again.<br/><button class="clickable-button" data-value="Save this Learning">Retry</button>'),null===(V=hi.current)||void 0===V||V.show({severity:"error",sticky:!1,className:I.toastError,content:r("div",e({className:I.toastContent},{children:[r("div",e({className:I.toastHeaderLeft},{children:[a(v,{className:I.iconError}),a("span",e({className:"".concat(I.toastTitle," ").concat(I.toastTitleError)},{children:"Error"}))]})),a("p",e({className:"".concat(I.toastMessage," ").concat(I.toastMessageError)},{children:L||409===(null==E?void 0:E.status)?"Similar rule already exists. Please update and try again.":(null==O?void 0:O.message)||(null==O?void 0:O.detail)||"Something went wrong while saving the learning!"}))]}))}),[3,10];case 10:return[2]}})})},Fr=function(e){return"string"!=typeof e?e:e.replace(/<conflicting_text>[\s\S]*?<\/conflicting_text>/g,"").replace(/<decision_strength>[\s\S]*?<\/decision_strength>/g,"").replace(/<conflicting_reasoning>[\s\S]*?<\/conflicting_reasoning>/g,"").replace(/<conflicting_gap>[\s\S]*?<\/conflicting_gap>/g,"").replace(/<\/?conflicting>/g,"").trim()};d(function(){Vi&&!fi&&(console.log("isSaveRule received from agent — calling publish APIs directly."),Pi(!1),ne||ie?Pr():!0===We.current?Gr():t(void 0,void 0,void 0,function(){var e,n,t,r,a,o,l,s;return i(this,function(u){switch(u.label){case 0:u.trys.push([0,5,,6]),ye(F),e=new Map,n=function(n){var t,r,a,o,l,u;return i(this,function(i){switch(i.label){case 0:if(t=n.toLowerCase().replace(/ /g,"_"),r=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===t||e.tier_name.toLowerCase()===t.replace(/_/g,"")}),!(a=null==r?void 0:r.tier_id))return e.set(t,null),[2,"continue"];o={question:et,orgName:Un.ORGANIZATION_NAME,scope_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a},i.label=1;case 1:return i.trys.push([1,3,,4]),[4,w(o)];case 2:return l=i.sent(),e.set(t,(null===(s=null==l?void 0:l.data)||void 0===s?void 0:s.id)||null),console.log("✅ Question saved for ".concat(n,":"),l),[3,4];case 3:return u=i.sent(),console.error("❌ Error saving question for ".concat(n,":"),u),e.set(t,null),[3,4];case 4:return[2]}})},t=0,r=fe.tiers,u.label=1;case 1:return t<r.length?(a=r[t],[5,n(a)]):[3,4];case 2:u.sent(),u.label=3;case 3:return t++,[3,1];case 4:return Vr(e),[3,6];case 5:return o=u.sent(),l=new Map,fe.tiers.forEach(function(e){l.set(e.toLowerCase().replace(/ /g,"_"),null)}),Vr(l),console.error("❌ Error saving Instant Learning Question:",o),[3,6];case 6:return[2]}})}))},[Vi,fi]);var qr=(null===(B=(null===(H=null==fe?void 0:fe.tiers)||void 0===H?void 0:H[0])||te||"Tier 1")||void 0===B?void 0:B.toLowerCase().replace(/ /g,"_"))||"tier_1",Ur=null!==(W=null!=xt?xt:re)&&void 0!==W?W:0,Jr=null!=xt,Hr=Jr?null==ft?void 0:ft[Ur]:null,Br=ft&&(null==ft?void 0:ft.find(function(e){return e&&!e.is_readOnly&&((null==e?void 0:e.new_variant_id)&&Ut.includes(e.new_variant_id)||(null==e?void 0:e.variant_id)&&Gt.includes(e.variant_id))})),Wr=(null==Hr?void 0:Hr.new_variant_id)&&Ut.includes(Hr.new_variant_id)?Hr.new_variant_id:(null==Br?void 0:Br.new_variant_id)&&Ut.includes(Br.new_variant_id)?Br.new_variant_id:"",Kr=!(!ne&&!ie),Yr=function(n){var t={},i=fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),r=(null==fe?void 0:fe.station)+"RuleSet",a=i.map(function(e){var n=je.find(function(n){return n.tier_name.toLowerCase().replace(/ /g,"_")===e||n.tier_name.toLowerCase()===e.replace(/_/g,"")});return(null==n?void 0:n.tier_id)||""}).filter(Boolean);return n.forEach(function(n,o){var l,s,u,d,c,_,v=(null==n?void 0:n.variant_id)&&n.variant_id||Gt[o]||"new_variant_".concat(o),p=(null===(l=n.scope)||void 0===l?void 0:l[0])||qr,f=ot.get(p),g=ut.get(p),m=_t.get(p),w=(null==f?void 0:f.get(o))||n.rule_description||"",y=(null==g?void 0:g.get(o))||[],h=y.length>0?y.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n "):n.wta||"",b=(null==m?void 0:m.get(o))||[],A=b.length>0?b.map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n "):n.wnta||"",T=Wn.get(p),M=null==T?void 0:T.get(o),I={};M&&Object.keys(M).length>0?I=e(e({},M),{order_rules:e(e({},M.order_rules),{global_min_separation_seconds:60*((null===(s=M.order_rules)||void 0===s?void 0:s.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},M.advertiser_rules),{global_min_separation_seconds:60*((null===(u=M.advertiser_rules)||void 0===u?void 0:u.global_min_separation_seconds)||0)})}):n.scheduling_config&&(I=n.scheduling_config),t[v]={rule_title:et,variant_description:w,wta:h,ruleset_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a,wnta:A,scope:i,scope_rule_set:r,scheduling_config:I,is_user_accepted_risk:!(null===(_=null!==(d=null==n?void 0:n.is_user_accepted_risk)&&void 0!==d?d:null===(c=(ft||[])[o])||void 0===c?void 0:c.is_user_accepted_risk)||void 0===_||!_)}}),t};bn&&console.log("customMetaData.ui_state for addvariant flow",Yr(bt));var zr=M(function(e){return e.selectedRuleSetId}),Qr={send_metadata_to_orchestrator:!1,default:["scope"],app_mapping_id:ve,ui_state:Kr?{variants:function(){var n={},t=Wn.get(qr);if(!t)return n;var i=ot.get(qr),r=ut.get(qr),a=_t.get(qr),o=fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),l=(null==fe?void 0:fe.station)+"RuleSet",s=o.map(function(e){var n=je.find(function(n){return n.tier_name.toLowerCase().replace(/ /g,"_")===e||n.tier_name.toLowerCase()===e.replace(/_/g,"")});return(null==n?void 0:n.tier_id)||""}).filter(Boolean);return t.forEach(function(u,d){var c,_,v,p,f,g,m=null===(c=null==ie?void 0:ie[d])||void 0===c?void 0:c.variantId,w=(ft||[])[d],y=(null==w?void 0:w.new_variant_id)||"",h=m||y||Gt[d]||"variant_".concat(d),b=(null==i?void 0:i.get(d))||"",A=((null==r?void 0:r.get(d))||[]).map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n ")||"",T=((null==a?void 0:a.get(d))||[]).map(function(e){return e.text}).filter(function(e){return""!==e.trim()}).join(" \\n ")||"",M=t.get(d),I={};M&&Object.keys(M).length>0&&(I=e(e({},M),{order_rules:e(e({},M.order_rules),{global_min_separation_seconds:60*((null===(_=M.order_rules)||void 0===_?void 0:_.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},M.advertiser_rules),{global_min_separation_seconds:60*((null===(v=M.advertiser_rules)||void 0===v?void 0:v.global_min_separation_seconds)||0)})})),n[h]={rule_title:et,variant_description:b,wta:A,ruleset_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:s,wnta:T,scope:o,scope_rule_set:l,scheduling_config:I,is_user_accepted_risk:!(null===(g=null!==(p=null==w?void 0:w.is_user_accepted_risk)&&void 0!==p?p:null===(f=null==ie?void 0:ie[d])||void 0===f?void 0:f.is_user_accepted_risk)||void 0===g||!g)}}),n}()}:function(){var n,t,i,r,a,o,l=(ft||[]).findIndex(function(e){return!0===e.is_new_variant}),s=-1;return-1!==l?s=l:fn?s=-1:bn||(s=Ur),{new_current_variant:-1!==s?{rule_title:et,rule_description:(null===(n=ot.get(qr))||void 0===n?void 0:n.get(s))||"",wta:(null===(i=null===(t=ut.get(qr))||void 0===t?void 0:t.get(s))||void 0===i?void 0:i.map(function(e){return Fr(e.text)}).filter(function(e){return""!==e}).join(" \\n "))||"",wnta:(null===(a=null===(r=_t.get(qr))||void 0===r?void 0:r.get(s))||void 0===a?void 0:a.map(function(e){return Fr(e.text)}).filter(function(e){return""!==e}).join(" \\n "))||"",scope:fe.tiers.map(function(e){return null==e?void 0:e.toLowerCase().replace(/ /g,"_")}),scope_rule_set:(null==fe?void 0:fe.station)+"RuleSet",scheduling_config:function(){var n,t,i,r=null===(n=Wn.get(qr))||void 0===n?void 0:n.get(s);return r&&0!==Object.keys(r).length?e(e({},r),{order_rules:e(e({},r.order_rules),{global_min_separation_seconds:60*((null===(t=r.order_rules)||void 0===t?void 0:t.global_min_separation_seconds)||0)}),advertiser_rules:e(e({},r.advertiser_rules),{global_min_separation_seconds:60*((null===(i=r.advertiser_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)})}):{}}(),is_user_accepted_risk:!!(null===(o=(ft||[])[s])||void 0===o?void 0:o.is_user_accepted_risk)}:{rule_title:"",rule_description:"",wta:"",wnta:"",scope:[],scope_rule_set:"",scheduling_config:{},is_user_accepted_risk:!1},variants:bn?Yr(bt):{}}}(),rule_id_in_current_session:St,new_variant_id:function(){var e;return Jr&&ne?(null===(e=null==ie?void 0:ie[Ur])||void 0===e?void 0:e.variantId)?null:Wr:null}(),edit_variant_id:On,rule_id:Rn.current,backward_compatibility:M.getState().backwardCompatibility,ruleset_id:zr,current_variants_length:ne?Array.isArray(ft)&&ft.length>0?Math.max((null===(K=null==ne?void 0:ne.variants)||void 0===K?void 0:K.length)||0,ft.length):null===(Y=null==ne?void 0:ne.variants)||void 0===Y?void 0:Y.length:void 0,tiers:M.getState().tiers,authToken:(null==or?void 0:or.accessToken)||""},Zr=Mr(bt),$r={ruleTitle:et,scopeData:fe,variantsByTier:Array.from(Wn.keys()).reduce(function(n,t){var i=Wn.get(t)||new Map,r=ot.get(t)||new Map,a=ut.get(t)||new Map,o=_t.get(t)||new Map;return console.log("tier config map",i),n[t]=Array.from(i.keys()).map(function(n,l){var s,u,d=(ft||[])[n]||{};console.log("Original item for tier",t,"index",n,":",d);var c={label:"",isDefault:!1};if(d.is_variant&&!U(d.variant_type))c={label:"",isDefault:!1};else if(ie&&ie.length>0){c=Rr(ie,n,"variantType"),console.log("Found matching variant at index",n,":",c);var _=ie[n];if(_){if(!c.label){var v=_.variantType||"",p=U(v);c={label:"variant ".concat(n+1),isDefault:p}}if(_.variantId===$i)(g=d.variant_type||(null==jt?void 0:jt.variant_type))&&(c=Dr(g,n),console.log("[templateState] editingVariants[".concat(n,']: using CARE variant_type "').concat(g,'" → label "').concat(c.label,'"')));console.log("Computed variant type at index",n,":",c)}else c={label:Mr(ie),isDefault:!1},console.log("No match found, using getNextVariantType:",c)}else if(bt&&bt.length>0){if(c=Rr(bt,n,"variant_type"),console.log("Found matching variant at index",n,":",c),We.current){var f=bt[n];if((null==f?void 0:f.variant_id)===rr||(null==f?void 0:f.variantId)===rr)(g=d.variant_type||(null==jt?void 0:jt.variant_type))&&(c=Dr(g,n),console.log("[templateState] existingVariants[".concat(n,']: load-and-edit using CARE variant_type "').concat(g,'" → label "').concat(c.label,'"')))}}else{var g=d.variant_type||"";c={label:Zr,isDefault:U(g)}}console.log("Computed variant data for tier",t,"index",n,":",c);var m=null!==(u=null===(s=i.get(n))||void 0===s?void 0:s.is_default_enabled)&&void 0!==u?u:c.isDefault;console.log("live is default :",m);var w="variant ".concat(l+1),y=r.get(n)||"";return{variantId:Gt[n]||d.variant_id||d.variantId,variantType:w,isDefault:m,isModified:d.is_modified,tier:t,index:n,ruleDescription:y.replace(/<ai_reasoning>[\s\S]*?<\/ai_reasoning>/g,""),whenToApplyConditions:(a.get(n)||[]).flatMap(function(n){var t,i=(null===(t=null==n?void 0:n.text)||void 0===t?void 0:t.split(/\s+OR\s+/))||[];return i.length>1?i.map(function(t,i){return e(e({},n),{id:"split-".concat(n.id,"-").concat(i),text:t.trim()})}):[n]}),whenNotToApplyConditions:(o.get(n)||[]).flatMap(function(n){var t,i=(null===(t=null==n?void 0:n.text)||void 0===t?void 0:t.split(/\s+OR\s+/))||[];return i.length>1?i.map(function(t,i){return e(e({},n),{id:"split-".concat(n.id,"-").concat(i),text:t.trim()})}):[n]}),configData:i.get(n)||Hn,isReadOnly:d.is_readOnly||!1,is_variant:d.is_variant||!1,reasoning:{ruleDescription:Sr(y),whenToApply:Sr(d.wta),whenNotToApply:Sr(d.wnta),configData:Sr(d.action)}}}),n},{}),citation:ii,fieldCitations:_i,citations:oi};console.log("templateState",$r);var Xr={setRuleTitle:function(n){var t,i=void 0!==(null===(t=null==n?void 0:n.target)||void 0===t?void 0:t.value)?n.target.value:n;nt(i),Ni(function(n){if(n.ruleTitle){var t=e({},n);return delete t.ruleTitle,t}return n})},setRuleDescription:function(e,n,t){return _r(e,n,t)},addWhenToApply:function(e,n){return vr(e,n)},removeWhenToApply:function(e,n,t){return pr(e,n,t)},editWhenToApply:function(e,n,t,i){return fr(e,n,t,i)},addWhenNotToApply:function(e,n){return gr(e,n)},removeWhenNotToApply:function(e,n,t){return mr(e,n,t)},editWhenNotToApply:function(e,n,t,i){return wr(e,n,t,i)},addNewVariant:function(e){return yr(e)},onScopeChange:br,onConfigChange:function(e,n,t,i){return Ar(e,n,t,i)}},ea={},na={};Wn.forEach(function(n,t){ea[t]=new Map,na[t]=new Map,n.forEach(function(n,i){ea[t].set(i,[e({key:"order_rules",label:"Order"},n.order_rules),e({key:"advertiser_rules",label:"Advertiser"},n.advertiser_rules)]),na[t].set(i,[{key:"inventory_rules",label:"Inventory code",enabled:n.inventory_rules.respect_inventory_code},{key:"break_code_rules",label:"Break Code",enabled:n.break_code_rules.respect_break_code},{key:"is_default_enabled",label:"Default Variant",enabled:n.is_default_enabled}])})});var ta=_(function(){if(te){if(1===fe.tiers.length)return 0;var e=fe.tiers.findIndex(function(e){return e.toLowerCase()===te.toLowerCase()});return e>=0?e:0}},[te,fe.tiers]),ia=e(e({defaultOpen:!0,tierOptions:["Tier 1","Tier 2"],stationOptions:[{label:"Global Scope",value:"global"},{label:"Genre Scope",value:"genre"},{label:"Custom Scope",value:"custom"}],separationItemsByTier:ea,togglesByTier:na,highlights:Yt,highlightedIds:Zt,openSections:zn,setOpenSections:Qn,setSendMessageInChat:Rt,isAiStreaming:fi,defaultTierOpen:ta,defaultVariantOpen:fn?void 0:xt,autoScrollVariantIndex:fn?void 0:xt,activeVariantAccordionIndex:fn?void 0:xt,setActiveVariantAccordionIndex:function(e){Et(e),null!=e&&setTimeout(function(){gn(!1)},100)},validationErrors:Ci,activeEditId:kn,setActiveEditId:Sn,pendingEditId:Nn,setPendingEditId:xn,conflictReason:Ei,usedFor:ne||ie?"Edit":"Create"},Jn),{scrollTrigger:ei});console.log(xt,"editVariantIndex checking"),d(function(){},[Q]),d(function(){if(!fi&&Mt&&!!(!ne&&!ie)){var e=n([],ft,!0).reverse().findIndex(function(e){return"variant"===e.action||e.is_variant});-1===e||ft.length,-1!==e&&(console.log("Post-stream: Focusing new variant at index",e),Et(e))}},[fi,Mt]);var ra=function(){Ee.current&&(clearTimeout(Ee.current),Ee.current=null),We.current=!1,Z(),Tr()},aa=function(){var e={};et.trim()||(e.ruleTitle="Rule Title is required"),fe.station||(e.station="Station Rule Set is required"),fe.tiers&&0!==fe.tiers.length||(e.tiers="At least one tier must be selected");return fe.tiers.map(function(e){return e.toLowerCase().replace(/ /g,"_")}).forEach(function(n){var t=ot.get(n),i=ut.get(n),r=Wn.get(n);r&&Array.from(r.keys()).forEach(function(r){var a=null==t?void 0:t.get(r);a&&a.trim()||(e["ruleDescription-".concat(n,"-").concat(r)]="Rule Description is required");var o=null==i?void 0:i.get(r);o&&o.some(function(e){return""!==e.text.trim()})||(e["whenToApply-".concat(n,"-").concat(r)]='At least one "When to apply" condition is required')})}),e};function oa(r,a,o){var l,s,u,d,c,_;return void 0===o&&(o=!1),t(this,void 0,void 0,function(){var t,v,p,f,g,m,w,y,h,A,T,M,I,k,S,C,N,x,E,O,L,R,D,j;return i(this,function(i){switch(i.label){case 0:if(t=r.question_id,v=(null==r?void 0:r.variant_id)||null,Tn.current&&jn.current===t)return console.log("[handleAILoadAction] Already fetching question ID, skipping duplicate request:",t),[2];if(Rn.current===t&&!o)return console.log("[handleAILoadAction] Question ID unchanged, updating variant focus only:",v),Ln(v),v?(-1!==(p=(ft||[]).findIndex(function(e){return e.is_new_variant}))&&(ki(!0),Hi("Removing new variant..."),Ki("Please wait while we remove new variant.")),gt(function(e){if(!Array.isArray(e))return e;var n=e.filter(function(e){return!e.is_new_variant}),t=n.findIndex(function(e){return e.variant_id===v});return-1!==t&&setTimeout(function(){Et(t),ni(function(e){return e+1}),yi.current=t},0),n}),-1!==p&&((f=function(e){e(function(e){var n=new Map;return e.forEach(function(e,t){var i=new Map(e);i.delete(p);var r=new Map,a=0;i.forEach(function(e){r.set(a,e),a++}),n.set(t,r)}),n})})(Kn),f(lt),f(dt),f(vt),Ft(function(e){var t=n([],e,!0);return t.length>p&&t.splice(p,1),t})),!1===Tn.current&&(Tn.current=!0,-1!==p&&(null===(l=hi.current)||void 0===l||l.show({severity:"success",summary:"Success",detail:"New variant removed successfully because you selected to edit an existing variant",life:4e3}))),gn(!1)):(console.log("lastLoadedQuestionIdRef.current in else",Rn.current),null===Rn.current&&(ki(!0),Hi("Adding new variant..."),Ki("Please wait while we add new variant."))),[2];console.log("User Action: fetching fresh variant info for question",t),Mn.current.clear(),Mn.current.add(t),ki(!0),Hi("Loading variants..."),console.log("reaching here in load and edit"),Ki("Please wait while we fetch the latest variant data."),Tn.current=!0,jn.current=t,i.label=1;case 1:return i.trys.push([1,5,6,10]),g=Date.now(),[4,b(t,(null==ee?void 0:ee.ruleSetId)||"")];case 2:return m=i.sent(),(w=Date.now()-g)<3e3?[4,new Promise(function(e){return setTimeout(e,3e3-w)})]:[3,4];case 3:i.sent(),i.label=4;case 4:return 0===(y=(null===(s=null==m?void 0:m.data)||void 0===s?void 0:s.variants)||[]).length?(console.warn("No variants found for question_id",t),ki(!1),Hi(""),null===(u=hi.current)||void 0===u||u.show({severity:"error",summary:"Error",detail:"No variants found for this question.",life:3e3}),[2]):(We.current=!0,Tn.current=!0,h=(null===(d=null==m?void 0:m.data)||void 0===d?void 0:d.ruleTitle)||"",nt(h),A=null===(c=null==m?void 0:m.data)||void 0===c?void 0:c.tier,T="tier_1",M="Tier 1",I="1",A&&((k=String(A).match(/(\d+)/))?(S=k[1],I=S,T="tier_".concat(S),M="Tier ".concat(S)):(T=String(A).toLowerCase().trim().replace(/ /g,"_"),M=T.replace(/_/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()}),I=T.includes("2")?"2":"1")),console.log("[handleAILoadAction] Detected Tier: ".concat(M," (ID: ").concat(I,", Key: ").concat(T,")")),ge({station:X||"global",tiers:[M]}),C=function(e,n,t,i,r,a){var o=e.map(function(e){var o,l,s=[t];return{variant_type:e.variantType||"variant",title:n,rule_description:e.description||"",wta:e.whenToApply||"",wnta:e.whenNotToApply||"",scope:s,scheduling_config:(null===(o=e.configuration)||void 0===o?void 0:o.scheduling_config)||{},variant_id:e.variantId,question_id:r,tier_id:i,is_readOnly:e.variantId!==a,link_id:e.link_id||"",is_enabled:null===(l=e.is_enabled)||void 0===l||l,is_user_accepted_risk:e.is_user_accepted_risk,has_accepted_risk:e.has_accepted_risk,user_accepted_risk_at:e.user_accepted_risk_at,action:"edit"}}),l=new Map,s=new Map,u=new Map,d=new Map;return o.forEach(function(e,n){var t,i,r,a,o,c,_=e.scheduling_config||{};l.set(n,{order_rules:{enabled:!!_.order_global_min_separation_seconds||!!(null===(t=_.order_rules)||void 0===t?void 0:t.global_min_separation_seconds),global_min_separation_seconds:Math.floor((_.order_global_min_separation_seconds||(null===(i=_.order_rules)||void 0===i?void 0:i.global_min_separation_seconds)||0)/60)},advertiser_rules:{enabled:!!_.advertiser_global_min_separation_seconds||!!(null===(r=_.advertiser_rules)||void 0===r?void 0:r.global_min_separation_seconds),global_min_separation_seconds:Math.floor((_.advertiser_global_min_separation_seconds||(null===(a=_.advertiser_rules)||void 0===a?void 0:a.global_min_separation_seconds)||0)/60)},inventory_rules:{respect_inventory_code:!!_.respect_inventory_code||!!(null===(o=_.inventory_rules)||void 0===o?void 0:o.respect_inventory_code)},break_code_rules:{respect_break_code:!!_.respect_break_code||!!(null===(c=_.break_code_rules)||void 0===c?void 0:c.respect_break_code)},is_default_enabled:U(e.variantType||e.variant_type)}),s.set(n,q(e.rule_description||""));var v=e.wta?e.wta.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];u.set(n,v);var p=e.wnta?e.wnta.split(" OR ").map(function(e){return e.trim()}).filter(function(e){return""!==e}).map(function(e,t){return{id:"edit-wnta-".concat(n,"-").concat(t),text:e,reasoning:""}}):[];d.set(n,p)}),{transformedVariants:o,tierConfigMap:l,tierDescMap:s,tierWtaMap:u,tierWntaMap:d}}(y,h,T,I,t,v),N=C.transformedVariants,x=C.tierConfigMap,E=C.tierDescMap,O=C.tierWtaMap,L=C.tierWntaMap,yt(function(e){var n=new Map(e);return n.set(T,JSON.parse(JSON.stringify(N))),n}),gt(function(e){var t=(Array.isArray(e)?e:[]).filter(function(e){return"copy"===e.action||e.tier_id&&e.tier_id!==I});return n(n([],t,!0),N,!0)}),At(function(e){var t=(Array.isArray(e)?e:[]).filter(function(e){return"copy"===e.action||e.tier_id&&e.tier_id!==I});return n(n([],t,!0),N,!0)}),An(!0),Rn.current=t,Ln(v),R=0,v&&-1!==(D=N.findIndex(function(e){return e.variant_id===v}))&&(R=D),Et(R),ni(function(e){return e+1}),yi.current=R,gn(!1),Kn(function(e){var n=new Map(e);return n.set(T,x),n}),lt(function(e){var n=new Map(e);return n.set(T,E),n}),dt(function(e){var n=new Map(e);return n.set(T,O),n}),vt(function(e){var n=new Map(e);return n.set(T,L),n}),Ni({}),[3,10]);case 5:return j=i.sent(),console.error("Failed to fetch variants by question ID:",j),null===(_=hi.current)||void 0===_||_.show({severity:"error",summary:"Error",detail:"Failed to fetch variants. Please try again.",life:3e3}),[3,10];case 6:return Tn.current=!1,ki(!1),Hi(""),Ki(""),(null==r?void 0:r.is_interpretation_ready)&&(null==r?void 0:r.question_id)&&Rn.current&&On?(console.log("inside edit variant flow",Rn.current,On),gt(function(n){if(!Array.isArray(n))return n;var t=n.map(function(n){return(null==n?void 0:n.variant_id)===On?e(e({},n),a):n}),i=n.findIndex(function(e){return(null==e?void 0:e.variant_id)===On});return console.log(i,"targetIndexif"),-1!==i&&setTimeout(function(){console.log("targetIndex",i),Et(i),ni(function(e){return e+1}),yi.current=i},0),t}),[3,9]):[3,7];case 7:return(null==r?void 0:r.is_interpretation_ready)&&(null==r?void 0:r.question_id)&&Rn.current&&!On?(console.log("inside add new variant flow",Rn.current,On),ki(!0),Hi("Adding new variant..."),Ki("Please wait while we add new variant."),[4,new Promise(function(e){return setTimeout(e,3e3)})]):[3,9];case 8:i.sent(),ki(!1),Hi(""),Ki(""),gt(function(t){var i;if(Array.isArray(t)){var r=t.findIndex(function(e){return!0===e.is_new_variant});if(-1!==r)setTimeout(function(){Et(r),ni(function(e){return e+1}),yi.current=r},0),i=t.map(function(n,t){return t===r?e(e(e({},n),a),{is_new_variant:!0,is_variant:!0}):n});else{var o=t.length;setTimeout(function(){Et(o),ni(function(e){return e+1}),yi.current=o},0),i=n(n([],t,!0),[e(e({},a),{is_new_variant:!0,is_variant:!0})],!1)}}else setTimeout(function(){Et(0),ni(0),yi.current=0},0),i=[e(e({},a),{is_new_variant:!0})];return Gn.current=i,i}),i.label=9;case 9:return[7];case 10:return[2]}})})}var la;return console.log("RuleSet in render",ee),r(o,{children:[a(g,{ref:hi}),a(k,{open:Q,chatAssistantName:Un.ASSISTANT_NAME,organizationName:Un.ORGANIZATION_NAME,baseUrl:Un.BASE_URL,baseWebsocketUrl:Un.BASE_WEBSOCKET_URL,closeFunction:ra,ThinkingBulbIconComponent:a(j,{}),customSections:a(o,{}),customMetaData:Qr,chatTitle:ne?Un.CHAT_TITLE_EDIT.replace("<scope>",(null==ee?void 0:ee.ruleSetName)||""):Un.CHAT_TITLE.replace("<scope>",(null==ee?void 0:ee.ruleSetName)||""),formTitle:Un.FORM_TITLE,showSavedMessage:!1,savedMessage:Me,isError:Ne,errorMessage:ke,isSaving:Le,onSave:function(e){console.log("Save this Learning",e);var n=aa();if(Object.keys(n).length>0)return console.log("validation errors",n),void Ni(n);Re(!0),xe(!1),Se("")},showSaveWithoutValidation:!1,onSaveWithoutValidation:function(){return t(void 0,void 0,void 0,function(){var e,n,t,r,a,o,l,s,u;return i(this,function(d){switch(d.label){case 0:return e=aa(),Object.keys(e).length>0?(Ni(e),[2]):(console.log("✅ Bypass Save: Initiated"),ne||ie?[4,Pr()]:[3,2]);case 1:return d.sent(),[3,11];case 2:return!0!==We.current?[3,4]:[4,Gr()];case 3:return d.sent(),[3,11];case 4:d.trys.push([4,10,,11]),ye(F),n=new Map,t=function(e){var t,r,a,o,l,s;return i(this,function(i){switch(i.label){case 0:if(t=e.toLowerCase().replace(/ /g,"_"),r=je.find(function(e){return e.tier_name.toLowerCase().replace(/ /g,"_")===t||e.tier_name.toLowerCase()===t.replace(/_/g,"")}),!(a=null==r?void 0:r.tier_id))return n.set(t,null),[2,"continue"];o={question:et,orgName:Un.ORGANIZATION_NAME,scope_id:(null==ee?void 0:ee.ruleSetId)||"",tier_id:a},i.label=1;case 1:return i.trys.push([1,3,,4]),[4,w(o)];case 2:return l=i.sent(),n.set(t,(null===(u=null==l?void 0:l.data)||void 0===u?void 0:u.id)||null),[3,4];case 3:return s=i.sent(),console.error("❌ Bypass Save: Error saving question for ".concat(e,":"),s),n.set(t,null),[3,4];case 4:return[2]}})},r=0,a=fe.tiers,d.label=5;case 5:return r<a.length?(o=a[r],[5,t(o)]):[3,8];case 6:d.sent(),d.label=7;case 7:return r++,[3,5];case 8:return[4,Vr(n,!0)];case 9:return d.sent(),[3,11];case 10:return l=d.sent(),ye(G),s=new Map,fe.tiers.forEach(function(e){s.set(e.toLowerCase().replace(/ /g,"_"),null)}),Vr(s,!0),console.error("❌ Bypass Save: Error",l),[3,11];case 11:return[2]}})})},headerMessageHandlers:kr,aiResponse:ft,setAIResponse:gt,width:"90vw",height:"90vh",sendMessageInChat:Lt,template:S.CreateRule,isLoading:we===F,templateProps:{state:$r,handlers:Xr,uiState:ia},setSendMessageInChat:Rt,onResetChat:function(){Tr()},onResetRequest:function(){Ti(!0)},isFormDirty:function(){return""!==et.trim()||""!==fe.station||fe.tiers.length>0||Array.from(ot.values()).some(function(e){return Array.from(e.values()).some(function(e){return""!==e.trim()})})||Array.from(ut.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.length>0})})||Array.from(_t.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.length>0})})||Wn.size>0||it.size>0},initialChatMessage:ne?(la=ne.ruleTitle||"this rule",null===(z=null==ie?void 0:ie[null!=re?re:0])||void 0===z||z.is_user_accepted_risk,'<p><span>Welcome back! 👋</span>\n <span>I see you want to edit <strong>"'.concat(la,'."</strong></span>\n <span> <strong>What would you like to update? </strong> </span>\n <span>Describe the change you want to make, and I’ll help you edit it.</span>\n </p>')):null!=_e?_e:Un.INITIAL_CHAT_MESSAGE,validateCustom:function(){if(we===P||be)return!1;if(!et||""===et.trim())return!1;if(null!==kn)return!1;if(!fe.station||""===fe.station.trim())return!1;var e=fe.tiers&&fe.tiers.filter(function(e){return e&&""!==e.trim()});if(!e||0===(null==e?void 0:e.length))return!1;for(var n,t=0,i=e.map(function(e){return e.toLowerCase().replace(/ /g,"_")});t<i.length;t++){var r=i[t],a=ot.get(r),o=ut.get(r),l=Wn.get(r);if(!l||0===l.size)return!1;for(var s=0,u=Array.from(l.keys());s<u.length;s++){var d=u[s],c=null==a?void 0:a.get(d);if(!c||""===c.trim())return!1;var _=null==o?void 0:o.get(d);if(!(null==_?void 0:_.some(function(e){return e.text&&""!==e.text.trim()})))return!1}}return!(!Wn||0===Wn.size)&&(!(n=function(e){return Array.from(e.values()).some(function(e){return Array.from(e.values()).some(function(e){return e.some(function(e){return!!e.conflict})})})})(ut)&&!n(_t)&&!Ze)},setCitations:li,isLocked:!0,textareaPlaceholder:"Tell me more about this learning..",saveButtonConfig:{label:ne?"Save Changes":"Save Learning",loadingLabel:r("span",e({style:{display:"flex",gap:"5px",alignItems:"center"}},{children:[a(p,{size:20,color:"white",className:"animate-spin"}),a("span",{children:" Saving Changes..."})]}))},setCurrentRequestId:di,isAiStreaming:fi,setIsAiStreaming:gi,showConfirmReset:Ai,setShowConfirmReset:Ti,confirmReset:function(){Tr(),Ti(!1),ra()},isLoadingVariants:Ii,mainText:Ji,subText:Wi,proposedRule:tn,isFetchingComparison:cr,handleOpenComparison:Ir,showComparison:Fi,setShowComparison:qi,variants:on,onComparisonLinkClick:dr,targetComparisonVariantId:un,highlightedComparisonField:_n,onAddAsVariant:function(){qi(!1)},isCopyEnabled:!0,onSubmitFeedback:function(n){return t(void 0,void 0,void 0,function(){var t,r,a,o,l;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),t=e({user_id:null!==(a=null==or?void 0:or._id)&&void 0!==a?a:"",user_name:null!==(o=null==or?void 0:or.firstname)&&void 0!==o?o:"",user_email:null!==(l=null==or?void 0:or.email)&&void 0!==l?l:"",assistant_name:Un.ASSISTANT_NAME},n),[4,A(t)];case 1:return i.sent(),[3,3];case 2:throw r=i.sent(),console.error("Failed to submit chat feedback",r),r;case 3:return[2]}})})},handleGlobalClickCapture:function(e){null!==kn&&(e.target.closest('[data-edit-id="'.concat(kn,'"]'))||(e.preventDefault(),e.stopPropagation(),xn(kn)))},isGlobalEditActive:null!==kn,initialDraftMessage:se?ce:"",socketTimeOutInMilliSeconds:Un.SOCKET_TIMEOUT})]})};export{H as default,q as filterAgentText};
2
2
  //# sourceMappingURL=InstantLearningModal.js.map