1import{W as G,r as v,k as S,n as L,H as R}from"./BUdAmU-I.js";function q(o){if(o.type==="link")return o.link||"#";if(o.type==="feature")switch(o.featureId){case"home":return"/";case"intro":return"/gioi-thieu";case"blog":return"/tin-tuc";case"contact":return"/lien-he";case"document":return"/van-ban";case"qa":return"/hoi-dap";default:return"#"}return"#"}const V=G("menu",()=>{const o=v([]),y=v(!0),d=v(!1),c=v(Date.now()),w=L(),A=S(()=>[...o.value].sort((e,r)=>(e.order??0)-(r.order??0)).map(e=>e.name)),N=S(()=>o.value.filter(e=>e.type==="link"&&e.children.length===0).sort((e,r)=>(e.order??0)-(r.order??0)).map(e=>({text:e.name,path:e.link||"#"}))),T=S(()=>e=>{const r=o.value.find(t=>t.name===e);return r?[...r.children].sort((t,i)=>(t.order??0)-(i.order??0)).map(t=>({id:t.id,name:t.name,path:q(t),children:t.children||[]})):[]});function u(e,r=o.value){for(const n of r){if(String(n.id)===String(e))return n;if(n.children&&n.children.length>0){const t=u(e,n.children);if(t)return t}}return null}function g(e,r=o.value){for(const n of r){if(n.children.some(t=>String(t.id)===String(e)))return n;if(n.children&&n.children.length>0){const t=g(e,n.children);if(t)return t}}return null}function s(e){const r={...e};return r.type==="link"?r.featureId=void 0:r.type==="feature"&&(r.link=void 0),r}function M(e){return{...s(e),children:e.children.map(n=>M(n))}}function h(e){return e.map(r=>({...s(r),children:Array.isArray(r.children)?h(r.children):[]}))}function f(e){o.value=h(e),c.value=Date.now()}function z(e){const r=s(e);o.value.push({...r,children:Array.isArray(e.children)?h(e.children):[]}),c.value=Date.now()}function $(e,r){const n=u(e);if(n){const t={...n,...r},i=s(t);Object.assign(n,{...i,children:r.children?h(r.children):n.children}),o.value=[...o.value],c.value=Date.now()}}function I(e){const r=[],n=u(e);if(!n)return r;const t=i=>{for(const l of i)r.push(l.id),l.children&&l.children.length>0&&t(l.children)};return t(n.children),r}function O(e){const r=g(e);r?r.children=r.children.filter(n=>String(n.id)!==String(e)):o.value=o.value.filter(n=>String(n.id)!==String(e)),c.value=Date.now()}function b(e,r){const n=u(e);if(n){const t=s({...r,children:[]});n.children.push(t),c.value=Date.now()}}function E(e,r,n){const t=u(e);if(t){const i=t.children.findIndex(l=>String(l.id)===String(r));if(i!==-1){const l={...t.children[i],...n},a=s(l),p=[...t.children];p[i]={...a,children:n.children?h(n.children):[...t.children[i].children]},t.children=p,c.value=Date.now()}}}function B(e,r){const n=u(e);n&&(n.children=n.children.filter(t=>String(t.id)!==String(r)),c.value=Date.now())}function k(e,r,n=-1){try{console.log(`[Store] Moving menu item ${e} to ${r===null?"root":"parent "+r} at index ${n}`);const t=u(e);if(!t)return console.error(`Menu item with id ${e} not found`),!1;const i=g(e),l=M(t);if(i?i.children=i.children.filter(a=>String(a.id)!==String(e)):o.value=o.value.filter(a=>String(a.id)!==String(e)),r===null)n>=0&&n<=o.value.length?o.value.splice(n,0,l):o.value.push(l);else{const a=u(r);
1if(!a)return console.error(`New parent menu with id ${r} not found`),i?i.children.push(l):o.value.push(l),!1;if(I(e).includes(r))return console.error(`Cannot move item ${e} to its own child ${r}`),i?i.children.push(l):o.value.push(l),!1;n>=0&&n<=a.children.length?a.children.splice(n,0,l):a.children.push(l)}return c.value=Date.now(),!0}catch(t){return console.error("Error moving menu item:",t),!1}}function J(e){try{console.log("[Store] Updating menu order with",e.length,"items");const r=[...e],n=new Map;o.value.forEach(t=>{n.set(String(t.id),[...t.children])}),r.forEach(t=>{n.has(String(t.id))&&(t.children=n.get(String(t.id)))}),o.value=r,c.value=Date.now(),console.log("[Store] Menu order updated, new order:",o.value.map(t=>({id:t.id,name:t.name})))}catch(r){console.error("[Store] Error updating menu order:",r)}}function D(e){const r=JSON.parse(JSON.stringify(e)),n=new Map;r.forEach(i=>{i.children=[],n.set(String(i.id),i)});const t=[];return r.forEach(i=>{const l=i.parent_id;if(l==null)t.push(i);else{const a=n.get(String(l));a?(delete i.parent_id,a.children.push(i)):t.push(i)}}),t}function U(e){const r=[],n=(t,i=null)=>{for(const l of t){const{children:a,...p}=l;r.push({...p,parent_id:i}),a&&a.length>0&&n(a,l.id)}};return n(e),r}function m(e){return JSON.parse(JSON.stringify(e)).map(n=>({id:n.id,name:n.name,type:n.type||"link",featureId:n.featureId||"",link:n.link||"",children:Array.isArray(n.children)?m(n.children):[],order:n.order||0}))}async function _(e){d.value=!0;try{if(Array.isArray(e)){f(m(e));return}const r=w.public.apiBase,n=await $fetch("/setting/menu-structure",{baseURL:r,method:"GET"}).catch(()=>null),t=n==null?void 0:n.data;if(n&&n.status==="success"&&Array.isArray(t))if(y.value=n.show_login_button!==!1,t.length>0&&t[0].children)f(m(t));else{const i=D(t);f(m(i))}else y.value=!0,f([{id:1,name:"GIá»I THIá»U",type:"feature",featureId:"intro",link:"",order:1,children:[{id:11,name:"Giá»i thiá»u chung",type:"link",featureId:"",link:"/gioi-thieu/chung",order:1,children:[]},{id:12,name:"CÆ¡ cấu tá» chức",type:"link",featureId:"",link:"/gioi-thieu/co-cau-to-chuc",order:2,children:[]}]},{id:2,name:"TIN TỨC - Sá»° KIá»N",type:"feature",featureId:"blog",link:"",order:2,children:[{id:21,name:"Tin tức má»i",type:"link",featureId:"",link:"/tin-tuc/moi",order:1,children:[]}]},{id:3,name:"Dá»CH VỤ CÃNG",type:"link",featureId:"",link:"https://dichvucong.binhduong.gov.vn/",order:3,children:[]},{id:4,name:"Há»I ÄÃP",type:"feature",featureId:"qa",link:"",order:4,children:[]}])}catch(r){console.error("Failed to fetch menu items:",r)}finally{d.value=!1}}async function H(){d.value=!0;try{const e=o.value;return await $fetch("/api/setting/menu-structure",{method:"POST",body:{menu_structure:e}}),!0}catch(e){return console.error("Failed to save menu items:",e),!1}finally{d.value=!1}}function C(){c.value=Date.now(),R(()=>{const e=JSON.parse(JSON.stringify(o.value));o.value=e,console.log("[Store] Menu refresh complete, items:",o.value.length)})}function F(e,r=null){var n,t,i;try{const l=((t=(n=e.item)==null?void 0:n.dataset)==null?void 0:t.menuId)||((i=e.item)==null?void 0:i.id);if(!l)return console.error("No item ID found in drag event:",e),!1;console.log(`[Store] Handling drag end for item ${l} to parent ${r}`);const a=k(l,r);return a&&C(),a}catch(l){return console.error("Error handling drag end:",l),!1}}return{menuItems:o,showLoginButton:y,isLoading:d,lastUpdated:c,mainMenus:A,staticLinks:N,getMenuChildren:T,findMenuById:u,findParentMenu:g,setMenuItems:f,addMenuItem:z,updateMenuItem:$,removeMenuItem:O,getChildrenIds:I,addChildMenuItem:b,updateChildMenuItem:E,removeChildMenuItem:B,moveMenuItem:k,updateMenuOrder:J,convertFlatToNested:D,convertNestedToFlat:U,fetchMenuItems:_,saveMenuItems:H,forceMenuRefresh:C,handleDragEnd:F}});export{V as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.