{"version":3,"file":"current.mjs","sources":["../../../../../../../packages/components/table/src/store/current.ts"],"sourcesContent":["// @ts-nocheck\r\nimport { getCurrentInstance, ref, unref } from 'vue'\r\nimport { getRowIdentity } from '../util'\r\n\r\nimport type { Ref } from 'vue'\r\nimport type { Table } from '../table/defaults'\r\nimport type { WatcherPropsData } from '.'\r\n\r\nfunction useCurrent<T>(watcherData: WatcherPropsData<T>) {\r\n  const instance = getCurrentInstance() as Table<T>\r\n  const _currentRowKey = ref<string>(null)\r\n  const currentRow: Ref<T> = ref(null)\r\n\r\n  const setCurrentRowKey = (key: string) => {\r\n    instance.store.assertRowKey()\r\n    _currentRowKey.value = key\r\n    setCurrentRowByKey(key)\r\n  }\r\n\r\n  const restoreCurrentRowKey = () => {\r\n    _currentRowKey.value = null\r\n  }\r\n\r\n  const setCurrentRowByKey = (key: string) => {\r\n    const { data, rowKey } = watcherData\r\n    let _currentRow = null\r\n    if (rowKey.value) {\r\n      _currentRow = (unref(data) || []).find(\r\n        (item) => getRowIdentity(item, rowKey.value) === key\r\n      )\r\n    }\r\n    currentRow.value = _currentRow\r\n    instance.emit('current-change', currentRow.value, null)\r\n  }\r\n\r\n  const updateCurrentRow = (_currentRow: T) => {\r\n    const oldCurrentRow = currentRow.value\r\n    if (_currentRow && _currentRow !== oldCurrentRow) {\r\n      currentRow.value = _currentRow\r\n      instance.emit('current-change', currentRow.value, oldCurrentRow)\r\n      return\r\n    }\r\n    if (!_currentRow && oldCurrentRow) {\r\n      currentRow.value = null\r\n      instance.emit('current-change', null, oldCurrentRow)\r\n    }\r\n  }\r\n\r\n  const updateCurrentRowData = () => {\r\n    const rowKey = watcherData.rowKey.value\r\n    // data 为 null 时，解构时的默认值会被忽略\r\n    const data = watcherData.data.value || []\r\n    const oldCurrentRow = currentRow.value\r\n    // 当 currentRow 不在 data 中时尝试更新数据\r\n    if (!data.includes(oldCurrentRow) && oldCurrentRow) {\r\n      if (rowKey) {\r\n        const currentRowKey = getRowIdentity(oldCurrentRow, rowKey)\r\n        setCurrentRowByKey(currentRowKey)\r\n      } else {\r\n        currentRow.value = null\r\n      }\r\n      if (currentRow.value === null) {\r\n        instance.emit('current-change', null, oldCurrentRow)\r\n      }\r\n    } else if (_currentRowKey.value) {\r\n      // 把初始时下设置的 rowKey 转化成 rowData\r\n      setCurrentRowByKey(_currentRowKey.value)\r\n      restoreCurrentRowKey()\r\n    }\r\n  }\r\n\r\n  return {\r\n    setCurrentRowKey,\r\n    restoreCurrentRowKey,\r\n    setCurrentRowByKey,\r\n    updateCurrentRow,\r\n    updateCurrentRowData,\r\n    states: {\r\n      _currentRowKey,\r\n      currentRow,\r\n    },\r\n  }\r\n}\r\n\r\nexport default useCurrent\r\n"],"names":[],"mappings":";;;AAEA,SAAS,UAAU,CAAC,WAAW,EAAE;AACjC,EAAE,MAAM,QAAQ,GAAG,kBAAkB,EAAE,CAAC;AACxC,EAAE,MAAM,cAAc,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;AACnC,EAAE,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;AAC/B,EAAE,MAAM,gBAAgB,GAAG,CAAC,GAAG,KAAK;AACpC,IAAI,QAAQ,CAAC,KAAK,CAAC,YAAY,EAAE,CAAC;AAClC,IAAI,cAAc,CAAC,KAAK,GAAG,GAAG,CAAC;AAC/B,IAAI,kBAAkB,CAAC,GAAG,CAAC,CAAC;AAC5B,GAAG,CAAC;AACJ,EAAE,MAAM,oBAAoB,GAAG,MAAM;AACrC,IAAI,cAAc,CAAC,KAAK,GAAG,IAAI,CAAC;AAChC,GAAG,CAAC;AACJ,EAAE,MAAM,kBAAkB,GAAG,CAAC,GAAG,KAAK;AACtC,IAAI,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,WAAW,CAAC;AACzC,IAAI,IAAI,WAAW,GAAG,IAAI,CAAC;AAC3B,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;AACtB,MAAM,WAAW,GAAG,CAAC,KAAK,CAAC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