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H,IAAI,cAAc,CAAA,GAAA,kBAAU,EAC1B,CAAC;QACC,kBAAkB;IACpB,GACA;QAAC;KAAkB;IAGrB,IAAI,uBAAuB,CAAA,GAAA,kBAAU,EACnC,CAAC,KAAU;QACT,IAAI,WAAW,aAAa,iBAAiB,CAC3C,MAAM,UAAU,EAChB,oBACA,KACA;QAEF,IAAI,MAAM,IAAI,IAAI,MAAM,IAAI,CAAC,qBAAqB,GAAG,CAAC,CAAC,CAAC,IAAI,GAAK;gBAAC;gBAAK,SAAS,GAAG,CAAC;aAAM;QAC1F,IAAI,GAAG,CAAC,KAAK;QACb,sBAAsB;QACtB,OAAO;IACT,GACA;QAAC;QAAqB;QAAuB;QAAc,MAAM,UAAU;QAAE;KAAmB;IAGlG,IAAI,YAAY,CAAA,GAAA,kBAAU,EAAE;QAC1B,kBAAkB;IACpB,GAAG;QAAC;KAAkB;IAEtB,IAAI,eAAe,CAAA,GAAA,cAAM,EACvB,IAAM,aAAa,iBAAiB,CAAC,YAAY,MAAM,UAAU,EAAE,YACnE;QAAC;QAAY,MAAM,UAAU;QAAE;QAAW;KAAa;IAGzD,OAAO,CAAA,GAAA,cAAM,EACX,IAAO,CAAA;4BACL;kCACA;yBACA;uBACA;YACA,gBAAgB,CAAC,MAAa,aAAa,cAAc,CAAC;YAC1D,mBAAmB,CAAC,MAAa,aAAa,iBAAiB,CAAC;YAChE,mBAAmB,CAAC,MAAa,aAAa,iBAAiB,CAAC;YAChE,YAAY;0BACZ;QACF,CAAA,GACA;QACE;QACA;QACA;QACA;QACA;QACA;QACA;KACD;AAEL","sources":["packages/react-stately/src/table/useTableColumnResizeState.ts"],"sourcesContent":["/*\n * Copyright 2022 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {ColumnSize} from './Column';\nimport {GridNode} from '../grid/GridCollection';\nimport {Key} from '@react-types/shared';\nimport {TableColumnLayout} from './TableColumnLayout';\nimport {TableState} from './useTableState';\nimport {useCallback, useMemo, useState} from 'react';\n\nexport interface TableColumnResizeStateProps<T> {\n  /**\n   * Current width of the table or table viewport that the columns\n   * should be calculated against.\n   */\n  tableWidth: number;\n  /** A function that is called to find the default width for a given column. */\n  getDefaultWidth?: (node: GridNode<T>) => ColumnSize | null | undefined;\n  /** A function that is called to find the default minWidth for a given column. */\n  getDefaultMinWidth?: (node: GridNode<T>) => ColumnSize | null | undefined;\n}\n\nexport interface TableColumnResizeState<T> {\n  /**\n   * Called to update the state that a resize event has occurred.\n   * Returns the new widths for all columns based on the resized column.\n   */\n  updateResizedColumns: (key: Key, width: number) => Map<Key, ColumnSize>;\n  /** Callback for when onColumnResize has started. */\n  startResize: (key: Key) => void;\n  /** Callback for when onColumnResize has ended. */\n  endResize: () => void;\n  /** Gets the current width for the specified column. */\n  getColumnWidth: (key: Key) => number;\n  /** Gets the current minWidth for the specified column. */\n  getColumnMinWidth: (key: Key) => number;\n  /** Gets the current maxWidth for the specified column. */\n  getColumnMaxWidth: (key: Key) => number;\n  /** Key of the currently resizing column. */\n  resizingColumn: Key | null;\n  /** A reference to the table state. */\n  tableState: TableState<T>;\n  /** A map of the current column widths. */\n  columnWidths: Map<Key, number>;\n}\n\n/**\n * Provides column width state management for a table component with column resizing support.\n * Handles building a map of column widths calculated from the table's width and any provided column\n * width information from the collection. In addition, it tracks the currently resizing column and\n * provides callbacks for updating the widths upon resize operations.\n *\n * @param props - Props for the table.\n * @param state - State for the table, as returned by `useTableState`.\n */\nexport function useTableColumnResizeState<T>(\n  props: TableColumnResizeStateProps<T>,\n  state: TableState<T>\n): TableColumnResizeState<T> {\n  let {getDefaultWidth, getDefaultMinWidth, tableWidth = 0} = props;\n\n  let [resizingColumn, setResizingColumn] = useState<Key | null>(null);\n  let columnLayout = useMemo(\n    () =>\n      new TableColumnLayout({\n        getDefaultWidth,\n        getDefaultMinWidth\n      }),\n    [getDefaultWidth, getDefaultMinWidth]\n  );\n\n  let [controlledColumns, uncontrolledColumns] = useMemo(\n    () => columnLayout.splitColumnsIntoControlledAndUncontrolled(state.collection.columns),\n    [state.collection.columns, columnLayout]\n  );\n\n  // uncontrolled column widths\n  let [uncontrolledWidths, setUncontrolledWidths] = useState(() =>\n    columnLayout.getInitialUncontrolledWidths(uncontrolledColumns)\n  );\n\n  // Update uncontrolled widths if the columns changed.\n  let [lastColumns, setLastColumns] = useState(state.collection.columns);\n  if (state.collection.columns !== lastColumns) {\n    if (\n      state.collection.columns.length !== lastColumns.length ||\n      state.collection.columns.some((c, i) => c.key !== lastColumns[i].key)\n    ) {\n      let newUncontrolledWidths = columnLayout.getInitialUncontrolledWidths(uncontrolledColumns);\n      setUncontrolledWidths(newUncontrolledWidths);\n    }\n    setLastColumns(state.collection.columns);\n  }\n\n  // combine columns back into one map that maintains same order as the columns\n  let colWidths = useMemo(\n    () =>\n      columnLayout.recombineColumns(\n        state.collection.columns,\n        uncontrolledWidths,\n        uncontrolledColumns,\n        controlledColumns\n      ),\n    [\n      state.collection.columns,\n      uncontrolledWidths,\n      uncontrolledColumns,\n      controlledColumns,\n      columnLayout\n    ]\n  );\n\n  let startResize = useCallback(\n    (key: Key) => {\n      setResizingColumn(key);\n    },\n    [setResizingColumn]\n  );\n\n  let updateResizedColumns = useCallback(\n    (key: Key, width: number): Map<Key, ColumnSize> => {\n      let newSizes = columnLayout.resizeColumnWidth(\n        state.collection,\n        uncontrolledWidths,\n        key,\n        width\n      );\n      let map = new Map(Array.from(uncontrolledColumns).map(([key]) => [key, newSizes.get(key)!]));\n      map.set(key, width);\n      setUncontrolledWidths(map);\n      return newSizes;\n    },\n    [uncontrolledColumns, setUncontrolledWidths, columnLayout, state.collection, uncontrolledWidths]\n  );\n\n  let endResize = useCallback(() => {\n    setResizingColumn(null);\n  }, [setResizingColumn]);\n\n  let columnWidths = useMemo(\n    () => columnLayout.buildColumnWidths(tableWidth, state.collection, colWidths),\n    [tableWidth, state.collection, colWidths, columnLayout]\n  );\n\n  return useMemo(\n    () => ({\n      resizingColumn,\n      updateResizedColumns,\n      startResize,\n      endResize,\n      getColumnWidth: (key: Key) => columnLayout.getColumnWidth(key),\n      getColumnMinWidth: (key: Key) => columnLayout.getColumnMinWidth(key),\n      getColumnMaxWidth: (key: Key) => columnLayout.getColumnMaxWidth(key),\n      tableState: state,\n      columnWidths\n    }),\n    [\n      columnLayout,\n      columnWidths,\n      resizingColumn,\n      updateResizedColumns,\n      startResize,\n      endResize,\n      state\n    ]\n  );\n}\n"],"names":[],"version":3,"file":"useTableColumnResizeState.mjs.map"}