import*as Platform from"../../platform/platform.js";import*as DataGridRenderers from"./DataGridRenderers.js";export function getRowEntryForColumnId(e,r){const t=e.cells.find((e=>e.columnId===r));if(void 0===t)throw new Error(`Found a row that was missing an entry for column ${r}.`);return t}export function renderCellValue(e){return e.renderer?e.renderer(e.value):DataGridRenderers.primitiveRenderer(e.value)}export function calculateColumnWidthPercentageFromWeighting(e,r){const t=e.filter((e=>e.visible)).reduce(((e,r)=>e+r.widthWeighting),0),n=e.find((e=>e.id===r));if(!n)throw new Error(`Could not find column with ID ${r}`);if(n.widthWeighting<1)throw new Error(`Error with column ${r}: width weightings must be >= 1.`);return n.visible?Math.round(n.widthWeighting/t*100):0}export function handleArrowKeyNavigation(e){const{key:r,currentFocusedCell:t,columns:n,rows:o}=e,[i,l]=t;switch(r){case"ArrowLeft":{if(i===n.findIndex((e=>e.visible)))return[i,l];let e=i;for(let r=e-1;r>=0;r--){if(n[r].visible){e=r;break}}return[e,l]}case"ArrowRight":{let e=i;for(let r=e+1;r<n.length;r++){if(n[r].visible){e=r;break}}return[e,l]}case"ArrowUp":{const e=n.some((e=>!0===e.sortable))?0:1;if(l===e)return[i,l];let r=l;for(let t=l-1;t>=e;t--){if(0===t){r=0;break}if(!o[t-1].hidden){r=t;break}}return[i,r]}case"ArrowDown":{if(0===l){const e=o.findIndex((e=>!e.hidden));return e>-1?[i,e+1]:[i,l]}let e=l;for(let r=e+1;r<o.length+1;r++){if(!o[r-1].hidden){e=r;break}}return[i,e]}default:return Platform.assertNever(r,`Unknown arrow key: ${r}`)}}export const calculateFirstFocusableCell=e=>{const{columns:r,rows:t}=e,n=r.some((e=>!0===e.sortable))?0:t.findIndex((e=>!e.hidden))+1;return[r.findIndex((e=>e.visible)),n]};export class ContextMenuColumnSortClickEvent extends Event{constructor(e){super("context-menu-column-sort-click"),this.data={column:e}}}export class ContextMenuHeaderResetClickEvent extends Event{constructor(){super("context-menu-header-reset-click")}}