import{noChange as t,html as i}from"lit";import{directive as s,PartType as h}from"lit/directive.js";import{AsyncDirective as e}from"lit/async-directive.js";import{repeat as n}from"lit/directives/repeat.js";
/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */class l extends Event{constructor(t){super(l.eventName,{bubbles:!1}),this.first=t.first,this.last=t.last}}l.eventName="rangeChanged";class o extends Event{constructor(t){super(o.eventName,{bubbles:!1}),this.first=t.first,this.last=t.last}}o.eventName="visibilityChanged";class r extends Event{constructor(){super(r.eventName,{bubbles:!1})}}r.eventName="unpinned";
/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */
class u{constructor(t){this._element=null;const i=t??window;this._node=i,t&&(this._element=t)}get element(){return this._element||document.scrollingElement||document.documentElement}get scrollTop(){return this.element.scrollTop||window.scrollY}get scrollLeft(){return this.element.scrollLeft||window.scrollX}get scrollHeight(){return this.element.scrollHeight}get scrollWidth(){return this.element.scrollWidth}get viewportHeight(){return this._element?this._element.getBoundingClientRect().height:window.innerHeight}get viewportWidth(){return this._element?this._element.getBoundingClientRect().width:window.innerWidth}get maxScrollTop(){return this.scrollHeight-this.viewportHeight}get maxScrollLeft(){return this.scrollWidth-this.viewportWidth}}class a extends u{constructor(t,i){super(i),this._clients=new Set,this._retarget=null,this._end=null,this.t=null,this.correctingScrollError=!1,this._checkForArrival=this._checkForArrival.bind(this),this._updateManagedScrollTo=this._updateManagedScrollTo.bind(this),this.scrollTo=this.scrollTo.bind(this),this.scrollBy=this.scrollBy.bind(this);const s=this._node;this._originalScrollTo=s.scrollTo,this._originalScrollBy=s.scrollBy,this._originalScroll=s.scroll,this._attach(t)}get _destination(){return this.t}get scrolling(){return null!==this._destination}scrollTo(t,i){const s="number"==typeof t&&"number"==typeof i?{left:t,top:i}:t;this._scrollTo(s)}scrollBy(t,i){const s="number"==typeof t&&"number"==typeof i?{left:t,top:i}:t;void 0!==s.top&&(s.top+=this.scrollTop),void 0!==s.left&&(s.left+=this.scrollLeft),this._scrollTo(s)}_nativeScrollTo(t){this._originalScrollTo.bind(this._element||window)(t)}_scrollTo(t,i=null,s=null){null!==this._end&&this._end(),"smooth"===t.behavior?(this._setDestination(t),this._retarget=i,this._end=s):this._resetScrollState(),this._nativeScrollTo(t)}_setDestination(t){let{top:i,left:s}=t;return i=void 0===i?void 0:Math.max(0,Math.min(i,this.maxScrollTop)),s=void 0===s?void 0:Math.max(0,Math.min(s,this.maxScrollLeft)),(null===this._destination||s!==this._destination.left||i!==this._destination.top)&&(this.t={top:i,left:s,behavior:"smooth"},!0)}_resetScrollState(){this.t=null,this._retarget=null,this._end=null}_updateManagedScrollTo(t){this._destination&&this._setDestination(t)&&this._nativeScrollTo(this._destination)}managedScrollTo(t,i,s){return this._scrollTo(t,i,s),this._updateManagedScrollTo}correctScrollError(t){this.correctingScrollError=!0,requestAnimationFrame((()=>requestAnimationFrame((()=>this.correctingScrollError=!1)))),this._nativeScrollTo(t),this._retarget&&this._setDestination(this._retarget()),this._destination&&this._nativeScrollTo(this._destination)}_checkForArrival(){if(null!==this._destination){const{scrollTop:t,scrollLeft:i}=this;let{top:s,left:h}=this._destination;s=Math.min(s||0,this.maxScrollTop),h=Math.min(h||0,this.maxScrollLeft);const e=Math.abs(s-t),n=Math.abs(h-i);e<1&&n<1&&(this._end&&this._end(),this._resetScrollState())}}detach(t){return this._clients.delete(t),0===this._clients.size&&(this._node.scrollTo=this._originalScrollTo,this._node.scrollBy=this._originalScrollBy,this._node.scroll=this._originalScroll,this._node.removeEventListener("scroll",this._checkForArrival)),null}_attach(t){this._clients.add(t),1===this._clients.size&&(this._node.scrollTo=this.scrollTo,this._node.scrollBy=this.scrollBy,this._node.scroll=this.scrollTo,this._node.addEventListener("scroll",this._checkForArrival))}}
/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */let c="undefined"!=typeof window?window.ResizeObserver:void 0;const d=Symbol("virtualizerRef"),f="virtualizer-sizer";let p;class w{constructor(t){if(this._benchmarkStart=null,this._layout=null,this._clippingAncestors=[],this._scrollSize=null,this._scrollError=null,this._childrenPos=null,this._childMeasurements=null,this._toBeMeasured=new Map,this._rangeChanged=!0,this._itemsChanged=!0,this._visibilityChanged=!0,this._scrollerController=null,this._isScroller=!1,this._sizer=null,this._hostElementRO=null,this._childrenRO=null,this._mutationObserver=null,this._scrollEventListeners=[],this._scrollEventListenerOptions={passive:!0},this._loadListener=this._childLoaded.bind(this),this._scrollIntoViewTarget=null,this._updateScrollIntoViewCoordinates=null,this._items=[],this._first=-1,this._last=-1,this._firstVisible=-1,this._lastVisible=-1,this._scheduled=new WeakSet,this._measureCallback=null,this._measureChildOverride=null,this._layoutCompletePromise=null,this._layoutCompleteResolver=null,this._layoutCompleteRejecter=null,this._pendingLayoutComplete=null,this._layoutInitialized=null,this._connected=!1,!t)throw new Error("Virtualizer constructor requires a configuration object");if(!t.hostElement)throw new Error('Virtualizer configuration requires the "hostElement" property');this._init(t)}set items(t){Array.isArray(t)&&t!==this._items&&(this._itemsChanged=!0,this._items=t,this._schedule(this._updateLayout))}_init(t){this._isScroller=!!t.scroller,this._initHostElement(t);const i=t.layout||{};this._layoutInitialized=this._initLayout(i)}_initObservers(){this._mutationObserver=new MutationObserver(this._finishDOMUpdate.bind(this)),this._hostElementRO=new c((()=>this._hostElementSizeChanged())),this._childrenRO=new c(this._childrenSizeChanged.bind(this))}_initHostElement(t){const i=this._hostElement=t.hostElement;this._applyVirtualizerStyles(),i[d]=this}connected(){this._initObservers();const t=this._isScroller;this._clippingAncestors=function(t,i=!1){let s=!1;return function(t,i=!1){const s=[];let h=i?t:g(t);for(;null!==h;)s.push(h),h=g(h);return s}(t,i).filter((t=>{if(s)return!1;const i=getComputedStyle(t);return s="fixed"===i.position,"visible"!==i.overflow}))}
/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */(this._hostElement,t),this._scrollerController=new a(this,this._clippingAncestors[0]),this._schedule(this._updateLayout),this._observeAndListen(),this._connected=!0}_observeAndListen(){this._mutationObserver.observe(this._hostElement,{childList:!0}),this._hostElementRO.observe(this._hostElement),this._scrollEventListeners.push(window),window.addEventListener("scroll",this,this._scrollEventListenerOptions),this._clippingAncestors.forEach((t=>{t.addEventListener("scroll",this,this._scrollEventListenerOptions),this._scrollEventListeners.push(t),this._hostElementRO.observe(t)})),this._hostElementRO.observe(this._scrollerController.element),this._children.forEach((t=>this._childrenRO.observe(t))),this._scrollEventListeners.forEach((t=>t.addEventListener("scroll",this,this._scrollEventListenerOptions)))}disconnected(){this._scrollEventListeners.forEach((t=>t.removeEventListener("scroll",this,this._scrollEventListenerOptions))),this._scrollEventListeners=[],this._clippingAncestors=[],this._scrollerController?.detach(this),this._scrollerController=null,this._mutationObserver?.disconnect(),this._mutationObserver=null,this._hostElementRO?.disconnect(),this._hostElementRO=null,this._childrenRO?.disconnect(),this._childrenRO=null,this._rejectLayoutCompletePromise("disconnected"),this._connected=!1}_applyVirtualizerStyles(){const t=this._hostElement.style;t.display=t.display||"block",t.position=t.position||"relative",t.contain=t.contain||"size layout",this._isScroller&&(t.overflow=t.overflow||"auto",t.minHeight=t.minHeight||"150px")}_getSizer(){const t=this._hostElement;if(!this._sizer){let i=t.querySelector(`[${f}]`);i||(i=document.createElement("div"),i.setAttribute(f,""),t.appendChild(i)),Object.assign(i.style,{position:"absolute",margin:"-2px 0 0 0",padding:0,visibility:"hidden",fontSize:"2px"}),i.textContent="&nbsp;",i.setAttribute(f,""),this._sizer=i}return this._sizer}async updateLayoutConfig(t){await this._layoutInitialized;const i=t.type||p;if("function"==typeof i&&this._layout instanceof i){const i={...t};return delete i.type,this._layout.config=i,!0}return!1}async _initLayout(t){let i,s;if("function"==typeof t.type){s=t.type;const h={...t};delete h.type,i=h}else i=t;void 0===s&&(p=s=(await import("./flow-8340c041.esm.js")).FlowLayout),this._layout=new s((t=>this._handleLayoutMessage(t)),i),this._layout.measureChildren&&"function"==typeof this._layout.updateItemSizes&&("function"==typeof this._layout.measureChildren&&(this._measureChildOverride=this._layout.measureChildren),this._measureCallback=this._layout.updateItemSizes.bind(this._layout)),this._layout.listenForChildLoadEvents&&this._hostElement.addEventListener("load",this._loadListener,!0),this._schedule(this._updateLayout)}startBenchmarking(){null===this._benchmarkStart&&(this._benchmarkStart=window.performance.now())}stopBenchmarking(){if(null!==this._benchmarkStart){const t=window.performance.now(),i=t-this._benchmarkStart,s=performance.getEntriesByName("uv-virtualizing","measure").filter((i=>i.startTime>=this._benchmarkStart&&i.startTime<t)).reduce(((t,i)=>t+i.duration),0);return this._benchmarkStart=null,{timeElapsed:i,virtualizationTime:s}}return null}_measureChildren(){const t={},i=this._children,s=this._measureChildOverride||this._measureChild;for(let h=0;h<i.length;h++){const e=i[h],n=this._first+h;(this._itemsChanged||this._toBeMeasured.has(e))&&(t[n]=s.call(this,e,this._items[n]))}this._childMeasurements=t,this._schedule(this._updateLayout),this._toBeMeasured.clear()}_measureChild(t){const{width:i,height:s}=t.getBoundingClientRect();return Object.assign({width:i,height:s},function(t){const i=window.getComputedStyle(t);return{marginTop:m(i.marginTop),marginRight:m(i.marginRight),marginBottom:m(i.marginBottom),marginLeft:m(i.marginLeft)}}(t))}async _schedule(t){this._scheduled.has(t)||(this._scheduled.add(t),await Promise.resolve(),this._scheduled.delete(t),t.call(this))}async _updateDOM(t){this._scrollSize=t.scrollSize,this._adjustRange(t.range),this._childrenPos=t.childPositions,this._scrollError=t.scrollError||null;const{_rangeChanged:i,_itemsChanged:s}=this;this._visibilityChanged&&(this._notifyVisibility(),this._visibilityChanged=!1),(i||s)&&(this._notifyRange(),this._rangeChanged=!1),this._finishDOMUpdate()}_finishDOMUpdate(){this._connected&&(this._children.forEach((t=>this._childrenRO.observe(t))),this._checkScrollIntoViewTarget(this._childrenPos),this._positionChildren(this._childrenPos),this._sizeHostElement(this._scrollSize),this._correctScrollError(),this._benchmarkStart&&"mark"in window.performance&&window.performance.mark("uv-end"))}_updateLayout(){this._layout&&this._connected&&(this._layout.items=this._items,this._updateView(),null!==this._childMeasurements&&(this._measureCallback&&this._measureCallback(this._childMeasurements),this._childMeasurements=null),this._layout.reflowIfNeeded(),this._benchmarkStart&&"mark"in window.performance&&window.performance.mark("uv-end"))}_handleScrollEvent(){if(this._benchmarkStart&&"mark"in window.performance){try{window.performance.measure("uv-virtualizing","uv-start","uv-end")}catch(t){console.warn("Error measuring performance data: ",t)}window.performance.mark("uv-start")}!1===this._scrollerController.correctingScrollError&&this._layout?.unpin(),this._schedule(this._updateLayout)}handleEvent(t){if("scroll"===t.type)(t.currentTarget===window||this._clippingAncestors.includes(t.currentTarget))&&this._handleScrollEvent();else console.warn("event not handled",t)}_handleLayoutMessage(t){"stateChanged"===t.type?this._updateDOM(t):"visibilityChanged"===t.type?(this._firstVisible=t.firstVisible,this._lastVisible=t.lastVisible,this._notifyVisibility()):"unpinned"===t.type&&this._hostElement.dispatchEvent(new r)}get _children(){const t=[];let i=this._hostElement.firstElementChild;for(;i;)i.hasAttribute(f)||t.push(i),i=i.nextElementSibling;return t}_updateView(){const t=this._hostElement,i=this._scrollerController?.element,s=this._layout;if(t&&i&&s){let h,e,n,l;const o=t.getBoundingClientRect();h=0,e=0,n=window.innerHeight,l=window.innerWidth;const r=this._clippingAncestors.map((t=>t.getBoundingClientRect()));r.unshift(o);for(const t of r)h=Math.max(h,t.top),e=Math.max(e,t.left),n=Math.min(n,t.bottom),l=Math.min(l,t.right);const u=i.getBoundingClientRect(),a={left:o.left-u.left,top:o.top-u.top},c={width:i.scrollWidth,height:i.scrollHeight},d=h-o.top+t.scrollTop,f=e-o.left+t.scrollLeft,p=Math.max(0,n-h),w=Math.max(0,l-e);s.viewportSize={width:w,height:p},s.viewportScroll={top:d,left:f},s.totalScrollSize=c,s.offsetWithinScroller=a}}_sizeHostElement(t){const i=82e5,s=t&&null!==t.width?Math.min(i,t.width):0,h=t&&null!==t.height?Math.min(i,t.height):0;if(this._isScroller)this._getSizer().style.transform=`translate(${s}px, ${h}px)`;else{const t=this._hostElement.style;t.minWidth=s?`${s}px`:"100%",t.minHeight=h?`${h}px`:"100%"}}_positionChildren(t){t&&t.forEach((({top:t,left:i,width:s,height:h,xOffset:e,yOffset:n},l)=>{const o=this._children[l-this._first];o&&(o.style.position="absolute",o.style.boxSizing="border-box",o.style.transform=`translate(${i}px, ${t}px)`,void 0!==s&&(o.style.width=s+"px"),void 0!==h&&(o.style.height=h+"px"),o.style.left=void 0===e?null:e+"px",o.style.top=void 0===n?null:n+"px")}))}async _adjustRange(t){const{_first:i,_last:s,_firstVisible:h,_lastVisible:e}=this;this._first=t.first,this._last=t.last,this._firstVisible=t.firstVisible,this._lastVisible=t.lastVisible,this._rangeChanged=this._rangeChanged||this._first!==i||this._last!==s,this._visibilityChanged=this._visibilityChanged||this._firstVisible!==h||this._lastVisible!==e}_correctScrollError(){if(this._scrollError){const{scrollTop:t,scrollLeft:i}=this._scrollerController,{top:s,left:h}=this._scrollError;this._scrollError=null,this._scrollerController.correctScrollError({top:t-s,left:i-h})}}element(t){return t===1/0&&(t=this._items.length-1),void 0===this._items?.[t]?void 0:{scrollIntoView:(i={})=>this._scrollElementIntoView({...i,index:t})}}_scrollElementIntoView(t){if(t.index>=this._first&&t.index<=this._last)this._children[t.index-this._first].scrollIntoView(t);else if(t.index=Math.min(t.index,this._items.length-1),"smooth"===t.behavior){const i=this._layout.getScrollIntoViewCoordinates(t),{behavior:s}=t;this._updateScrollIntoViewCoordinates=this._scrollerController.managedScrollTo(Object.assign(i,{behavior:s}),(()=>this._layout.getScrollIntoViewCoordinates(t)),(()=>this._scrollIntoViewTarget=null)),this._scrollIntoViewTarget=t}else this._layout.pin=t}_checkScrollIntoViewTarget(t){const{index:i}=this._scrollIntoViewTarget||{};i&&t?.has(i)&&this._updateScrollIntoViewCoordinates(this._layout.getScrollIntoViewCoordinates(this._scrollIntoViewTarget))}_notifyRange(){this._hostElement.dispatchEvent(new l({first:this._first,last:this._last}))}_notifyVisibility(){this._hostElement.dispatchEvent(new o({first:this._firstVisible,last:this._lastVisible}))}get layoutComplete(){return this._layoutCompletePromise||(this._layoutCompletePromise=new Promise(((t,i)=>{this._layoutCompleteResolver=t,this._layoutCompleteRejecter=i}))),this._layoutCompletePromise}_rejectLayoutCompletePromise(t){null!==this._layoutCompleteRejecter&&this._layoutCompleteRejecter(t),this._resetLayoutCompleteState()}_scheduleLayoutComplete(){this._layoutCompletePromise&&null===this._pendingLayoutComplete&&(this._pendingLayoutComplete=requestAnimationFrame((()=>requestAnimationFrame((()=>this._resolveLayoutCompletePromise())))))}_resolveLayoutCompletePromise(){null!==this._layoutCompleteResolver&&this._layoutCompleteResolver(),this._resetLayoutCompleteState()}_resetLayoutCompleteState(){this._layoutCompletePromise=null,this._layoutCompleteResolver=null,this._layoutCompleteRejecter=null,this._pendingLayoutComplete=null}_hostElementSizeChanged(){this._schedule(this._updateLayout)}_childLoaded(){}_childrenSizeChanged(t){if(this._layout?.measureChildren){for(const i of t)this._toBeMeasured.set(i.target,i.contentRect);this._measureChildren()}this._scheduleLayoutComplete(),this._itemsChanged=!1,this._rangeChanged=!1}}function m(t){const i=t?parseFloat(t):NaN;return Number.isNaN(i)?0:i}function g(t){if(null!==t.assignedSlot)return t.assignedSlot;if(null!==t.parentElement)return t.parentElement;const i=t.parentNode;return i&&i.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&i.host||null}const v=s(class extends e{constructor(t){if(super(t),this._virtualizer=null,this._first=0,this._last=-1,this._renderItem=(t,s)=>((t,s)=>i`${s}: ${JSON.stringify(t,null,2)}`)(t,s+this._first),this._keyFunction=(t,i)=>(t=>t)(t,this._first),this._items=[],t.type!==h.CHILD)throw new Error("The virtualize directive can only be used in child expressions")}render(t){t&&this._setFunctions(t);const i=[];if(this._first>=0&&this._last>=this._first)for(let t=this._first;t<=this._last;t++)i.push(this._items[t]);return n(i,this._keyFunction,this._renderItem)}update(i,[s]){this._setFunctions(s);const h=this._items!==s.items;return this._items=s.items||[],this._virtualizer?this._updateVirtualizerConfig(i,s):this._initialize(i,s),h?t:this.render()}async _updateVirtualizerConfig(t,i){if(!await this._virtualizer.updateLayoutConfig(i.layout||{})){const s=t.parentNode;this._makeVirtualizer(s,i)}this._virtualizer.items=this._items}_setFunctions(t){const{renderItem:i,keyFunction:s}=t;i&&(this._renderItem=(t,s)=>i(t,s+this._first)),s&&(this._keyFunction=(t,i)=>s(t,i+this._first))}_makeVirtualizer(t,i){this._virtualizer&&this._virtualizer.disconnected();const{layout:s,scroller:h,items:e}=i;this._virtualizer=new w({hostElement:t,layout:s,scroller:h}),this._virtualizer.items=e,this._virtualizer.connected()}_initialize(t,i){const s=t.parentNode;s&&1===s.nodeType&&(s.addEventListener("rangeChanged",(t=>{this._first=t.first,this._last=t.last,this.setValue(this.render())})),this._makeVirtualizer(s,i))}disconnected(){this._virtualizer?.disconnected()}reconnected(){this._virtualizer?.connected()}});export{v};
