{"version":3,"file":"main.Cry5yo.js","sources":["theme-178/virtual:theme","theme-178/discourse/api-initializers/sidebar-menu-reorder-init.js","theme-178/virtual:entrypoint:main"],"sourcesContent":["import { getObjectForTheme } from \"discourse/lib/theme-settings-store\";\n\nexport const settings = getObjectForTheme(178);\n\nexport function themePrefix(key) {\n  return `theme_translations.178.${key}`;\n}\n","import { apiInitializer } from \"discourse/lib/api\";\nimport { schedule } from \"@ember/runloop\";\n\nexport default apiInitializer(\"0.11.1\", (api) => {\n  // Observer instances for tracking DOM changes\n  let sidebarObserver = null;      // Watches .sidebar-sections for section changes\n  let containerObserver = null;    // Watches app container for sidebar creation/destruction\n  let reorderQueued = false;       // Prevents multiple simultaneous reorder operations\n\n  /**\n   * Main reordering function that applies flexbox order to sidebar sections\n   * based on the sidebar_menu_order setting. Also extracts nested custom sections\n   * and makes them direct children of .sidebar-sections for proper ordering.\n   */\n  function reorderSidebar() {\n    reorderQueued = false;\n\n    // Parse the pipe-delimited setting (e.g., \"categories|tags|community\")\n    const desiredOrder = settings.sidebar_menu_order.split('|');\n    const totalInOrder = desiredOrder.length;\n    const sectionsContainer = document.querySelector('.sidebar-sections');\n    if (!sectionsContainer) { return; }\n\n    // Apply flexbox layout to enable CSS order property\n    sectionsContainer.style.display = 'flex';\n    sectionsContainer.style.flexDirection = 'column';\n\n    // Collect all sections, tracking whether they're direct children or nested\n    const allSections = [];\n    \n    sectionsContainer.querySelectorAll('[data-section-name]').forEach(sectionElement => {\n      const sectionName = sectionElement.dataset.sectionName;\n      \n      // Only process sections explicitly listed in settings\n      // Unlisted sections (e.g., user-created private sections) stay in default position\n      if (!desiredOrder.includes(sectionName)) {\n        return;\n      }\n      \n      // Walk up the DOM tree to find the direct child of sectionsContainer\n      // This is needed because custom sections are often nested in wrapper divs\n      let flexItem = sectionElement;\n      while (flexItem && flexItem.parentElement !== sectionsContainer) {\n        flexItem = flexItem.parentElement;\n      }\n      \n      if (flexItem) {\n        allSections.push({\n          sectionName,\n          sectionElement,      // The actual section element with [data-section-name]\n          flexItem,            // The direct child of sectionsContainer\n          isDirectChild: sectionElement === flexItem  // True if no wrapper exists\n        });\n      }\n    });\n\n    // Apply CSS order values and extract nested sections\n    allSections.forEach(({ sectionName, sectionElement, flexItem, isDirectChild }) => {\n      const orderIndex = desiredOrder.indexOf(sectionName);\n      let calculatedOrder = 99;  // Default order for sections not in settings list\n      \n      // Use negative orders so sections appear before any default-ordered items\n      // Example: if totalInOrder is 3 and orderIndex is 0, calculatedOrder = -3\n      if (orderIndex !== -1) {\n        calculatedOrder = orderIndex - totalInOrder;\n      }\n\n      if (isDirectChild) {\n        // Section is already a direct child, just apply order\n        flexItem.style.setProperty('order', calculatedOrder, 'important');\n      } else {\n        // Section is nested (likely a custom section), extract it and make it a direct child\n        // This allows custom sections to be ordered alongside default sections\n        if (sectionElement.parentElement !== sectionsContainer) {\n          sectionsContainer.appendChild(sectionElement);\n        }\n        sectionElement.style.setProperty('order', calculatedOrder, 'important');\n        \n        // Add visual separator border to extracted custom sections\n        // (CSS removes this from the last child via :last-child selector)\n        sectionElement.style.borderBottom = '1px solid var(--d-sidebar-section-border-color)';\n      }\n    });\n  }\n\n  /**\n   * Queues a reorder operation using requestAnimationFrame to batch multiple\n   * rapid changes (e.g., during initial render) into a single reorder pass.\n   */\n  function queueReorder() {\n    if (!reorderQueued) {\n      reorderQueued = true;\n      window.requestAnimationFrame(reorderSidebar);\n    }\n  }\n\n  /**\n   * Sets up a MutationObserver on .sidebar-sections to watch for:\n   * - New sections being added\n   * - Sections being removed\n   * - Section attributes changing (class, style)\n   * \n   * This catches most sidebar changes but misses the sidebar being destroyed/recreated.\n   */\n  function setupObserver() {\n    const sectionsContainer = document.querySelector('.sidebar-sections');\n    if (!sectionsContainer) {\n      // Sidebar not ready yet, retry in 50ms\n      setTimeout(setupObserver, 50);\n      return;\n    }\n\n    // Disconnect existing observer to avoid duplicates\n    if (sidebarObserver) {\n      sidebarObserver.disconnect();\n    }\n    \n    sidebarObserver = new MutationObserver(queueReorder);\n    sidebarObserver.observe(sectionsContainer, {\n      childList: true,           // Watch for sections added/removed\n      subtree: true,             // Watch nested elements (for custom sections)\n      attributes: true,          // Watch attribute changes\n      attributeFilter: ['class', 'style']  // Only care about class/style changes\n    });\n\n    // Trigger initial reorder\n    queueReorder();\n  }\n\n  /**\n   * Sets up a higher-level MutationObserver on the app container to detect\n   * when the entire sidebar is created or destroyed (e.g., on collapse/expand).\n   * \n   * This solves the issue where collapsing and expanding the sidebar would\n   * reset sections to default order, as Discourse recreates the sidebar DOM.\n   */\n  function setupContainerObserver() {\n    // Watch the main outlet or fall back to body\n    const appContainer = document.querySelector('#main-outlet-wrapper') || document.body;\n    \n    if (containerObserver) {\n      containerObserver.disconnect();\n    }\n    \n    containerObserver = new MutationObserver(() => {\n      // Check if sidebar exists and set up section-level observer\n      const sectionsContainer = document.querySelector('.sidebar-sections');\n      if (sectionsContainer) {\n        setupObserver();\n      }\n    });\n    \n    containerObserver.observe(appContainer, {\n      childList: true,  // Watch for sidebar being added/removed\n      subtree: true     // Watch deeply nested changes\n    });\n    \n    // If sidebar already exists on init, set up immediately\n    if (document.querySelector('.sidebar-sections')) {\n      setupObserver();\n    }\n  }\n\n  // Re-setup observers when navigating to a new page (Discourse is a SPA)\n  api.onAppEvent('page:changed', () => {\n    schedule('afterRender', setupObserver);\n  });\n\n  // Initial setup after Ember's render cycle completes\n  schedule('afterRender', setupContainerObserver);\n});\n","import * as Mod1 from \"./discourse/api-initializers/sidebar-menu-reorder-init\";\nconst compatModules = {\n  \"discourse/api-initializers/sidebar-menu-reorder-init\": Mod1,\n};\nexport default compatModules;\n"],"names":["settings","getObjectForTheme","sidebarMenuReorderInit","apiInitializer","api","sidebarObserver","containerObserver","reorderQueued","reorderSidebar","desiredOrder","sidebar_menu_order","split","totalInOrder","length","sectionsContainer","document","querySelector","style","display","flexDirection","allSections","querySelectorAll","forEach","sectionElement","sectionName","dataset","includes","flexItem","parentElement","push","isDirectChild","orderIndex","indexOf","calculatedOrder","setProperty","appendChild","borderBottom","queueReorder","window","requestAnimationFrame","setupObserver","disconnect","MutationObserver","observe","childList","subtree","attributes","attributeFilter","setTimeout","onAppEvent","schedule","appContainer","body","compatModules"],"mappings":"yNAEaA,EAAWC,EAAkB;ACC1C,IAAAC,EAAeC,EAAe,SAAWC,IAEvC,IAAIC,EAAkB,KAClBC,EAAoB,KACpBC,GAAgB;AAOpB,SAASC,IACPD,GAAgB;AAGhB,MAAME,EAAeT,EAASU,mBAAmBC,MAAM,KACjDC,EAAeH,EAAaI,OAC5BC,EAAoBC,SAASC,cAAc;AACjD,IAAKF,EAAqB;AAG1BA,EAAkBG,MAAMC,QAAU,OAClCJ,EAAkBG,MAAME,cAAgB;AAGxC,MAAMC,EAAc;AAEpBN,EAAkBO,iBAAiB,uBAAuBC,QAAQC,IAChE,MAAMC,EAAcD,EAAeE,QAAQD;AAI3C,IAAKf,EAAaiB,SAASF,GACzB;AAKF,IAAIG,EAAWJ;AACf,KAAOI,GAAYA,EAASC,gBAAkBd,GAC5Ca,EAAWA,EAASC;AAGlBD,GACFP,EAAYS,KAAK,CACfL,cACAD,iBACAI,WACAG,cAAeP,IAAmBI,MAMxCP,EAAYE,QAAQ,EAAGE,cAAaD,iBAAgBI,WAAUG,oBAC5D,MAAMC,EAAatB,EAAauB,QAAQR;AACxC,IAAIS,EAAkB,IAIH,IAAfF,IACFE,EAAkBF,EAAanB,GAG7BkB,EAEFH,EAASV,MAAMiB,YAAY,QAASD,EAAiB,cAIjDV,EAAeK,gBAAkBd,GACnCA,EAAkBqB,YAAYZ,GAEhCA,EAAeN,MAAMiB,YAAY,QAASD,EAAiB,aAI3DV,EAAeN,MAAMmB,aAAe,oDAG1C,CAMA,SAASC,IACF9B,IACHA,GAAgB,EAChB+B,OAAOC,sBAAsB/B,GAEjC,CAUA,SAASgC,IACP,MAAM1B,EAAoBC,SAASC,cAAc;AAC5CF,GAODT,GACFA,EAAgBoC,aAGlBpC,EAAkB,IAAIqC,iBAAiBL,GACvChC,EAAgBsC,QAAQ7B,EAAmB,CACzC8B,WAAW,EACXC,SAAS,EACTC,YAAY,EACZC,gBAAiB,CAAC,QAAS,WAI7BV,KAlBEW,WAAWR,EAAe,GAmB9B,CAqCApC,EAAI6C,WAAW,eAAgB,KAC7BC,EAAS,cAAeV,KAI1BU,EAAS,cAjCT,WAEE,MAAMC,EAAepC,SAASC,cAAc,yBAA2BD,SAASqC;AAE5E9C,GACFA,EAAkBmC,aAGpBnC,EAAoB,IAAIoC,iBAAiB,KAEb3B,SAASC,cAAc,sBAE/CwB,MAIJlC,EAAkBqC,QAAQQ,EAAc,CACtCP,WAAW,EACXC,SAAS,IAIP9B,SAASC,cAAc,sBACzBwB,GAEJ;AChKG,MAACa,EAAgB,CACpB;"}