import{bS as R,bT as U,ao as t,ap as I,bp as z,fO as D,aI as C,bc as E,aC as N,aO as V,L as v,bq as K,aq as y,v as A,az as B,ak as j,bN as M,ge as W,ay as $,p as J,c4 as Q,dL as X,d as Y,m as w}from"./app-k0jcl5PQ.js";import{P as Z}from"./BpwqQXZV.chunk.js";import{a as H}from"./BcYgJJPz.chunk.js";import{O,B as _}from"./BzSUSuAL.chunk.js";import{u as ee}from"./Drs278fZ.chunk.js";import{P as te}from"./Kb3MNbLP.chunk.js";import"./DwDhXSrj.chunk.js";import"./EHuWazu9.chunk.js";import"./CCtuxSlD.chunk.js";import"./CPXlV0fg.chunk.js";import"./ApI7cEru.chunk.js";import"./DXxHdeNz.chunk.js";import"./CpGXOT1_.chunk.js";import"./DkDkjje4.chunk.js";import"./hKqzfsO4.chunk.js";import"./fstW5ZQE.chunk.js";import"./9ls1F1Ve.chunk.js";import"./DRKUY7jf.chunk.js";function se(e){const s=e.subject.cover?R.createUrl(e.subject.cover.url,{size:U.Small}):"";return t.jsx("li",{className:"col-md-4 col-12",children:t.jsx(H,{data:e.subject,coverUrl:s,isSelected:e.selected,onClick:e.onClick,className:I("h-auto"),multiline:!0},e.subject.id)})}function ne(e){const{subjects:s,preferredPresentation:n,allSubjectsSelected:a,allSubjectsText:o,selectedSubjects:g,altSelectionText:f,onSelectAll:h}=e,l=z(),P=s.reduce((r,c)=>r.concat(c.children.length>0?c.children:[c]),[]);return t.jsxs(t.Fragment,{children:[t.jsx(H,{data:{name:o},coverUrl:D.getPresentationLevelImage(n.learningLevel,"cover"),isSelected:a,onClick:h,multiline:!0}),t.jsxs("div",{className:"d-flex align-items-center gap-3 py-3",children:[t.jsx("hr",{className:"flex-grow-1 m-0 text-gray-900"}),t.jsx("span",{children:f}),t.jsx("hr",{className:"flex-grow-1 m-0"})]}),t.jsx("ul",{className:I((l.sm||l.xs)&&"mx-auto","w-100 px-0 row g-3 list-unstyled"),children:P.map(r=>t.jsx(se,{subject:r,onClick:()=>e.onSelect(r),selected:!e.hasSelectedBulk&&g.includes(r.id),hasSelectedBulk:e.hasSelectedBulk},r.id))})]})}const k="subjects.subjectPreferences";function ae(e){if(!(e!=null&&e.length))return[];const s=[];return e.forEach(n=>{if(!n.children||!n.children.length){s.push({...n});return}s.push({...n,isExpandableOption:!0,children:[{...n},...n.children]})}),s}function ie(e){var T;const[s,n]=C.useState([]),[a,o]=C.useState(e.bulkPreferencesState),[g,f]=C.useState(!1),h=E(),l=O.hasBulkSelectedTopics(a);C.useEffect(()=>{var i;if(!((i=e.preselections)!=null&&i.length)){n([]);return}n(N.uniq([...s,...e.preselections.map(u=>u.id)]))},[(T=e.preselections)==null?void 0:T.length]);const[P,r]=ee(()=>{f(!1),e.isOnboarding||h.success(v.getPhrase(k,"success")),e.nextAppLink&&A.trigger(e.nextAppLink)});function c(i){let u=s;a===_.AllSubjects&&(u=[],b());const d=N.addOrRemoveImmutable(u,i.id);n(d)}function S(i){return e.preselections?e.preselections.map(d=>d.id).sort().toString()!==i.sort().toString():!1}function m(){return e.bulkPreferencesState!==a}function x(){if(!s.length&&!l)return!1;const i=m()||S(s);return!(!e.isOnboarding&&!i)}function p(){if(!x())return;if(!S(s)&&!m()){e.nextAppLink&&A.trigger(e.nextAppLink);return}f(!0);const i=s.filter(L=>!e.preselections.find(G=>G.id===L)),u=e.preselections.filter(L=>!s.includes(L.id));let d=0;i.length&&d++,u.length&&d++,m()&&d++,r(d),e.saveChanges(i,u,a,P,()=>{h.error(v.getPhrase(k,"error")),f(!1)})}function b(){const i=O.getNextBulkState(a,"topics");o(i)}const F=ae(e.options);return t.jsx(V.Provider,{value:k,children:t.jsxs("form",{children:[t.jsx(ne,{subjects:F,onSelect:c,hasSelectedBulk:l,selectedSubjects:s,preferredPresentation:e.preferredPresentation,allSubjectsSelected:l,allSubjectsText:v.getPhrase(k,"allSubjects"),altSelectionText:v.getPhrase(k,"optionalSelection"),onSelectAll:b}),t.jsx(K,{variant:"dark",disabled:!x(),onClick:p,showSpinner:g,children:t.jsx(y,{phrase:e.isOnboarding?"next":"save"})})]})})}function re(e){const s=a=>a?a.filter(o=>o.canBePreferred):[],n=B(j.mySubjectPreferences(a=>{}));return{...n,isFetching:n.isFetching,data:s(n.data)}}const q="settings.subjects",ce=v.encloseNamespace(q);function Le(){M({title:ce("pageTitle")});const e=B(j.availablePresentations()),s=B(j.allLevelPreferences()),n=re(),{preferredPresentation:a}=W(),o=B(j.allSubjectPreferences()),{bulkOnboardingPreferencesState:g,isLoadingSettings:f,updateUserSetting:h}=$(J.PreferencesSettings),l=Q();function P(c,S,m,x,p){if(m!==g){const b={name:Y.BulkOnboardingPreferencesState,value:m,kind:X.Setting};h(b,x,p)}c!=null&&c.length&&w(l,j.followSubjects(c,x,p)),S!=null&&S.length&&w(l,j.unfollowSubjects(S.map(b=>b.id),x,p))}const r=!!o.hasCompleted&&!!n.hasCompleted&&!f;return t.jsxs(te,{availablePresentations:e.data,audiences:s.data,children:[t.jsx("p",{className:"mt-3",children:t.jsx(y,{namespace:q,phrase:"info"})}),r?t.jsx(ie,{options:o.data,preselections:n.data,bulkPreferencesState:g,saveChanges:P,preferredPresentation:a}):t.jsx(Z,{count:7})]})}export{Le as SubjectProfileView};
//# sourceMappingURL=lrSN1RLQ.chunk.js.map
