{"version":3,"file":"helper.mjs","names":[],"sources":["../../../../../../../packages/components/table/src/store/helper.ts"],"sourcesContent":["import { watch } from 'vue'\nimport { debounce } from 'lodash-unified'\nimport { isObject } from '@element-plus/utils'\nimport useStore from '.'\n\nimport type { Store } from '.'\nimport type { DefaultRow, Table, TableProps } from '../table/defaults'\n\nconst InitialStateMap = {\n  rowKey: 'rowKey',\n  defaultExpandAll: 'defaultExpandAll',\n  rowExpandable: 'rowExpandable',\n  selectOnIndeterminate: 'selectOnIndeterminate',\n  indent: 'indent',\n  lazy: 'lazy',\n  ['treeProps.hasChildren']: {\n    key: 'lazyColumnIdentifier',\n    default: 'hasChildren',\n  },\n  ['treeProps.children']: {\n    key: 'childrenColumnName',\n    default: 'children',\n  },\n  ['treeProps.checkStrictly']: {\n    key: 'checkStrictly',\n    default: false,\n  },\n}\n\nexport function createStore<T extends DefaultRow>(\n  table: Table<T>,\n  props: TableProps<T>\n) {\n  if (!table) {\n    throw new Error('Table is required.')\n  }\n\n  const store = useStore<T>()\n  // fix https://github.com/ElemeFE/element/issues/14075\n  // related pr https://github.com/ElemeFE/element/pull/14146\n  store.toggleAllSelection = debounce(store._toggleAllSelection, 10)\n  Object.keys(InitialStateMap).forEach((key) => {\n    handleValue(getArrKeysValue(props, key), key, store)\n  })\n  proxyTableProps(store, props)\n  return store\n}\n\nfunction proxyTableProps<T extends DefaultRow>(\n  store: Store<T>,\n  props: TableProps<T>\n) {\n  Object.keys(InitialStateMap).forEach((key) => {\n    watch(\n      () => getArrKeysValue(props, key),\n      (value) => {\n        handleValue(value, key, store)\n      }\n    )\n  })\n}\n\nfunction handleValue<T extends DefaultRow>(\n  value: string | boolean | Record<string, any>,\n  propsKey: string,\n  store: Store<T>\n) {\n  let newVal = value\n  let storeKey = InitialStateMap[propsKey as keyof typeof InitialStateMap]\n  if (isObject(storeKey)) {\n    newVal = newVal || storeKey.default\n    storeKey = storeKey.key\n  }\n  ;((store.states as any)[storeKey] as any).value = newVal\n}\n\nfunction getArrKeysValue<T extends DefaultRow>(\n  props: TableProps<T>,\n  key: string\n) {\n  if ((key as keyof typeof props).includes('.')) {\n    const keyList = (key as keyof typeof props).split('.')\n    let value: string | Record<string, any> = props\n    keyList.forEach((k) => {\n      value = (value as Record<string, any>)[k]\n    })\n    return value\n  } else {\n    return (props as any)[key] as boolean | string\n  }\n}\n"],"mappings":";;;;;AAQA,MAAM,kBAAkB;CACtB,QAAQ;CACR,kBAAkB;CAClB,eAAe;CACf,uBAAuB;CACvB,QAAQ;CACR,MAAM;EACL,0BAA0B;EACzB,KAAK;EACL,SAAS;EACV;EACA,uBAAuB;EACtB,KAAK;EACL,SAAS;EACV;EACA,4BAA4B;EAC3B,KAAK;EACL,SAAS;EACV;CACF;AAED,SAAgB,YACd,OACA,OACA;CACA,IAAI,CAAC,OACH,MAAM,IAAI,MAAM,qBAAqB;CAGvC,MAAM,QAAQ,UAAa;CAG3B,MAAM,qBAAqB,SAAS,MAAM,qBAAqB,GAAG;CAClE,OAAO,KAAK,gBAAgB,CAAC,SAAS,QAAQ;EAC5C,YAAY,gBAAgB,OAAO,IAAI,EAAE,KAAK,MAAM;GACpD;CACF,gBAAgB,OAAO,MAAM;CAC7B,OAAO;;AAGT,SAAS,gBACP,OACA,OACA;CACA,OAAO,KAAK,gBAAgB,CAAC,SAAS,QAAQ;EAC5C,YACQ,gBAAgB,OAAO,IAAI,GAChC,UAAU;GACT,YAAY,OAAO,KAAK,MAAM;IAEjC;GACD;;AAGJ,SAAS,YACP,OACA,UACA,OACA;CACA,IAAI,SAAS;CACb,IAAI,WAAW,gBAAgB;CAC/B,IAAI,SAAS,SAAS,EAAE;EACtB,SAAS,UAAU,SAAS;EAC5B,WAAW,SAAS;;CAErB,MAAQ,OAAe,UAAkB,QAAQ;;AAGpD,SAAS,gBACP,OACA,KACA;CACA,IAAK,IAA2B,SAAS,IAAI,EAAE;EAC7C,MAAM,UAAW,IAA2B,MAAM,IAAI;EACtD,IAAI,QAAsC;EAC1C,QAAQ,SAAS,MAAM;GACrB,QAAS,MAA8B;IACvC;EACF,OAAO;QAEP,OAAQ,MAAc"}