import store from "@/store";
import { baseURL } from "./http";

const filesLookFu = (name: string, url?: string) => {
  let flag = false;

  if (name.toLowerCase().endsWith(".pdf")) {
    if (url) window.open(baseURL + url);
    flag = true;
  }

  const arr1 = [".png", ".jpg", ".jpeg", ".gif"];
  arr1.forEach((v) => {
    if (name.toLowerCase().endsWith(v)) {
      if (url) {
        store.dispatch({
          type: "layout/lookBigImg",
          payload: {
            url: baseURL + url,
            show: true,
          },
        });
      }

      flag = true;
    }
  });

  let type: "" | "video" | "audio" = "";

  const arr2 = [".mp3", ".wav"];

  arr2.forEach((v) => {
    if (name.toLowerCase().endsWith(v)) {
      type = "audio";
      flag = true;
    }
  });

  if (name.toLowerCase().endsWith(".mp4")) {
    type = "video";
    flag = true;
  }

  if (type && url) {
    store.dispatch({
      type: "layout/lookDom",
      payload: {
        src: url,
        type,
      },
    });
  }

  return flag;
};

export default filesLookFu;
