import { DictListTypeObj, PermissionsAPIType } from "@/types";
import { MessageType } from "@/utils/message";

// 初始化状态
const initState = {
  // 所有图片点击预览查看大图
  lookBigImg: {
    url: "",
    show: false,
  },
  // 视频的src
  videoSrc: "",
  // 所有的下拉框数据
  dictList: {
    age: [],
    texture: [],
    level: [],
    source: [],
  } as DictListTypeObj,
  // 有关权限的信息
  authPageArr: [] as PermissionsAPIType[],
  // antd轻提示(兼容360浏览器)
  message: {
    txt: "",
    type: "info",
    duration: 3,
  } as MessageType,
  // 上传文件点击取消
  closeUpFile: {
    fu: () => {},
    state: false,
  },
};

// 定义 action 类型
type LoginActionType =
  | { type: "login/lookBigImg"; payload: { url: string; show: boolean } }
  | { type: "login/lookVideo"; payload: string }
  | { type: "login/getDictList"; payload: DictListTypeObj }
  | { type: "login/setAuthPageArr"; payload: PermissionsAPIType[] }
  | { type: "login/message"; payload: MessageType }
  | {
      type: "login/closeUpFile";
      payload: {
        fu: () => void;
        state: boolean;
      };
    };

// 频道 reducer
export default function loginReducer(
  state = initState,
  action: LoginActionType
) {
  switch (action.type) {
    // 所有图片点击预览查看大图
    case "login/lookBigImg":
      return { ...state, lookBigImg: action.payload };
    // 查看视频
    case "login/lookVideo":
      return { ...state, videoSrc: action.payload };
    // 所有的下拉框数据
    case "login/getDictList":
      return { ...state, dictList: action.payload };
    // antd轻提示(兼容360浏览器)
    case "login/message":
      return { ...state, message: action.payload };
    // 有关权限的信息
    case "login/setAuthPageArr":
      return { ...state, authPageArr: action.payload };
    // 上传文件点击取消
    case "login/closeUpFile":
      return { ...state, closeUpFile: action.payload };
    default:
      return state;
  }
}
