import*as Platform from"../../platform/platform.js";import*as LitHtml from"../../third_party/lit-html/lit-html.js";export function isExpandableNode(e){return"children"in e}export const trackDOMNodeToTreeNode=LitHtml.directive(((e,t)=>n=>{if(!(n instanceof LitHtml.AttributePart))throw new Error("Ref directive must be used as an attribute.");const r=n.committer.element;if(!(r instanceof HTMLLIElement))throw new Error("trackTreeNodeToDOMNode must be used on <li> elements.");e.set(r,t)}));const findNextParentSibling=e=>{const t=e.parentElement?.parentElement;if(t&&t instanceof HTMLLIElement){const e=t.nextElementSibling;return e&&e instanceof HTMLLIElement?e:findNextParentSibling(t)}return null},getFirstChildOfExpandedTreeNode=e=>{const t=e.querySelector(':scope > [role="group"] > [role="treeitem"]:first-child');if(!t)throw new Error("Could not find child of expanded node.");return t},domNodeIsExpandable=e=>null!==e.getAttribute("aria-expanded"),domNodeIsLeafNode=e=>!domNodeIsExpandable(e),domNodeIsExpanded=e=>domNodeIsExpandable(e)&&"true"===e.getAttribute("aria-expanded"),getDeepLastChildOfExpandedTreeNode=e=>{const t=e.querySelector(':scope > [role="group"] > [role="treeitem"]:last-child');if(!t)throw new Error("Could not find child of expanded node.");return domNodeIsExpanded(t)?getDeepLastChildOfExpandedTreeNode(t):t},getNextSiblingOfCurrentDOMNode=e=>{const t=e.nextElementSibling;return t&&t instanceof HTMLLIElement?t:null},getPreviousSiblingOfCurrentDOMNode=e=>{const t=e.previousElementSibling;return t&&t instanceof HTMLLIElement?t:null},getParentListItemForDOMNode=e=>{let t=e.parentElement;if(!t)return null;for(;t&&"treeitem"!==t.getAttribute("role")&&t instanceof HTMLLIElement===!1;)t=t.parentElement;return t};export const findNextNodeForTreeOutlineKeyboardNavigation=e=>{const{currentDOMNode:t,currentTreeNode:n,direction:r,setNodeExpandedState:o}=e;if(!n)return t;if("ArrowDown"===r){if(domNodeIsExpanded(t))return getFirstChildOfExpandedTreeNode(t);const e=getNextSiblingOfCurrentDOMNode(t);if(e)return e;const n=findNextParentSibling(t);if(n)return n}else{if("ArrowRight"===r)return domNodeIsExpandable(t)?domNodeIsExpanded(t)?getFirstChildOfExpandedTreeNode(t):(o(n,!0),t):t;if("ArrowUp"===r){const e=getPreviousSiblingOfCurrentDOMNode(t);if(e)return domNodeIsExpanded(e)?getDeepLastChildOfExpandedTreeNode(e):e;const n=getParentListItemForDOMNode(t);if(n&&n instanceof HTMLLIElement)return n}else if("ArrowLeft"===r){if(domNodeIsExpanded(t))return o(n,!1),t;const e=getParentListItemForDOMNode(t);if(e&&e instanceof HTMLLIElement)return e}}return t};