import type { CascaderItem } from "./Cascader.vue";

export function getCascaderText(
  value: (number|string)[], 
  valueKey: string, 
  textKey: string, 
  childrenKey: string,
  data: CascaderItem[],
) {
  const selectTexts : string[] = [];
  let currentGroup : CascaderItem[]|undefined = data;
  let i = 0;
  for (; i < value.length; i++) {
    const item : CascaderItem|undefined = currentGroup?.find(item => item[valueKey] === value[i]);
    if (item) {
      selectTexts.push(item[textKey]);
      currentGroup = item[childrenKey];
    }
  }
  if (currentGroup !== undefined) {
    const item : CascaderItem|undefined = currentGroup?.find(item => item[valueKey] === value[i]);
    if (item)
      selectTexts.push(item[textKey]);
  }
  return selectTexts.join(' / ');
}
export function getCascaderItemByValue(
  value: (number|string)[], 
  valueKey: string, 
  childrenKey: string,
  data: CascaderItem[],
) {
  const selectItems : CascaderItem[] = [];
  let currentGroup : CascaderItem[]|undefined = data;
  let i = 0;
  for (; i < value.length; i++) {
    const item : CascaderItem|undefined = currentGroup?.find(item => item[valueKey] === value[i]);
    if (item) {
      selectItems.push(item);
      currentGroup = item[childrenKey];
    }
  }
  if (currentGroup !== undefined) {
    const item : CascaderItem|undefined = currentGroup?.find(item => item[valueKey] === value[i]);
    if (item)
      selectItems.push(item);
  }
  return selectItems;
}