import e from"@babel/runtime/helpers/slicedToArray";import{useRef as t,useState as r,useMemo as i,useEffect as s}from"react";import n from"classnames";import a from"prop-types";import{sum as l,startCase as o,get as c,isEmpty as u}from"lodash";import{makeStyles as d,List as p,ListSubheader as m,Chip as h,ListItem as f,Checkbox as g,Typography as x}from"@material-ui/core";import{StatusOptions as b,ROWS_PER_PAGE as v}from"../../../constants.js";import{SEARCH_CATEGORIES as y}from"./constants.js";import{jsxs as R,jsx as A}from"react/jsx-runtime";var I=d((function(e){return{assetListWrapper:{margin:"8px 0",flex:1},assetListContainer:{maxHeight:"350px",backgroundColor:e.palette.background.paper,position:"relative",overflow:"auto"},listSection:{backgroundColor:"inherit"},ul:{backgroundColor:"inherit",padding:0},listGutters:{padding:"2px 8px"},assetTypeSubheaderChip:{height:"24px",color:e.palette.primary.text6},assetItemTextRoot:{minWidth:"300px"},assetItemTextPrimary:{overflow:"hidden",whiteSpace:"nowrap",textOverflow:"ellipsis"},loadingInfo:{textAlign:"center",fontSize:"12px",lineHeight:"17px",color:e.palette.primary.text8},empty:{color:e.palette.primary.text6,fontSize:"12px",fontStyle:"italic",marginLeft:"8px"},menuItem:{display:"flex",alignItems:"center",justifyContent:"space-between",width:"100%"},allMenuItem:{boxShadow:"inset 0px -1px 0px rgba(255, 255, 255, 0.25)"},statusWrapper:{display:"flex",alignItems:"center",width:"102px"},status:{width:"8px",height:"8px",marginRight:"8px",borderRadius:"50%",cursor:"default"}}})),k=function(a){var d=a.subjectWellId,k=a.page,C=a.assetType,S=a.loading,N=a.groupedAssets,T=a.onClickAsset,w=a.onClickAllAsset,H=a.onScrollReached,q=I(),W=t(null),j=r("top"),L=e(j,2),_=L[0],G=L[1],M=r(0),z=e(M,2),O=z[0],P=z[1],Y=l(null==N?void 0:N.map((function(t){var r=e(t,2);r[0];var i=r[1];return(null==i?void 0:i.length)||0}))),B=i((function(){if(1===N.length){var e=N[0][1],t=e.filter((function(e){return e.checked})).length;return[t>0&&e.length!==t,t===e.length]}return[!1,!1]}),[N]),D=e(B,2),E=D[0],F=D[1];s((function(){S?P(Y):("top"===_&&W.current.scrollTo(0,5),"bottom"===_&&W.current.scrollTo(0,W.current.scrollHeight-W.current.offsetHeight-5),"auto"===_&&W.current.scrollTo(0,(W.current.scrollHeight-W.current.offsetHeight)*O/Y))}),[S]);return R("div",{className:q.assetListWrapper,children:[R(p,{className:q.assetListContainer,subheader:A("li",{}),onScroll:function(t){if(t.target.scrollHeight!==t.target.offsetHeight){var r=t.target.scrollTop+t.target.offsetHeight;Math.abs(t.target.scrollHeight-r)<1&&!function(t,r){return!t.find((function(t){var i=e(t,2);return i[0],i[1].length===v*r}))}(N,k)&&(G("auto"),H("bottom"))}},ref:W,children:[N.map((function(t){var r=e(t,2),i=r[0],s=r[1];return A("li",{className:q.listSection,children:R("ul",{className:q.ul,children:[!C&&A(m,{classes:{gutters:q.listGutters},children:A(h,{className:q.assetTypeSubheaderChip,variant:"outlined",label:y[i].labelPlural},i)}),C&&A(f,{button:!0,classes:{gutters:q.listGutters},onClick:function(){return w(!F,i)},children:A("div",{className:n(q.menuItem,q.allMenuItem),children:R("div",{children:[A(g,{checked:F,indeterminate:E,style:{marginRight:8}}),"All"]})})}),s.map((function(e){var t;return A(f,{"data-testid":"".concat("AssetResultSection","_item_").concat(e.name,"MenuItem"),button:!0,classes:{gutters:q.listGutters},disabled:e.type===y.well.name&&!e.visibility.includes("visible")||!e.redirectAssetId||e.redirectAssetId===d,onClick:function(){return T(e,i)},children:R("div",{className:q.menuItem,children:[R("div",{children:[A(g,{checked:e.checked,style:{marginRight:8}}),e.name]}),R("div",{className:q.statusWrapper,children:[A("div",{className:q.status,style:{background:null===(t=b.find((function(t){return t.value===e.status})))||void 0===t?void 0:t.color}}),o(e.status)]})]})},"item-".concat(i,"-").concat(c(e,"asset_id")?c(e,"asset_id"):c(e,"id")))}))]})},"section-".concat(i))})),u(N)&&A("div",{className:q.empty,children:"Your search did not match any Asset Name"})]}),S&&A(x,{"data-testid":"".concat("AssetResultSection","_loading"),className:q.loadingInfo,children:"Loading..."})]})};k.propTypes={subjectWellId:a.number.isRequired,page:a.number.isRequired,assetType:a.string.isRequired,loading:a.bool.isRequired,groupedAssets:a.arrayOf(a.shape({})).isRequired,onClickAsset:a.func.isRequired,onClickAllAsset:a.func.isRequired,onScrollReached:a.func.isRequired};export{k as default};
