import { ref, computed } from "vue";
import { TDepartmentList } from "@mobileUI/typings/BCDepartmentSelectorPopup";

const useStack = () => {
  const stack = ref([]);

  //如果栈有两条数据就表示需要返回
  const showBack = computed(() => stack.value.length > 1);

  //当前显示在列表里的
  const currentList = computed(() => stack.value[stack.value.length - 1]);

  //添加到stack
  const onAddStack = (list: TDepartmentList[]) => stack.value.push(list);

  //从stack中移除一个
  const onPopStack = () => stack.value.pop();

  /**
   * 修改stack对应下标的列表
   * order默认是stack最后一位
   */
  const onChangeStack = (list: TDepartmentList[], order = stack.value.length - 1) => {
    stack.value[order] = list;
  };

  return {
    stack,
    showBack,
    currentList,
    onPopStack,
    onAddStack,
    onChangeStack,
  };
};

export default useStack;
