import { createRouter, createWebHashHistory } from "vue-router";
import pubsub from "pubsub-js";
import store from "@/store";
import { saveBackSnaps } from "../lib/useBackSnaps";
// import eventBus from '@/eventBus'
// import Home from "../views/Home.vue";

const routes = [
  {
    path: "/",
    redirect: "/discover/recommend",
    name: "index",
    // component: ()=> import('@/views/Home.vue'),
    // meta:{
    //   title:'zmusic',
    // }
  },
  {
    path: "/discover",
    name: "discover",
    component: () => import("../views/discover/Discover.vue"),
    meta: {
      keepAlive: true,
    },
    children: [
      {
        path: "recommend",
        name: "discover.recommend",
        component: () => import("@/views/discover/Recommend.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "songlist",
        name: "discover.songlist",
        component: () => import("../views/discover/Songlist.vue"),
      },
      {
        path: "anchor",
        name: "discover.anchor",
        component: () => import("../views/discover/Anchor.vue"),
      },
      {
        path: "ranking",
        name: "discover.ranking",
        component: () => import("../views/discover/Ranking.vue"),
      },
      {
        path: "singer",
        name: "discover.singer",
        component: () => import("../views/discover/Singer.vue"),
      },
      {
        path: "latest",
        name: "discover.latest",
        component: () => import("../views/discover/Latest.vue"),
      },
    ],
  },
  {
    path: "/fm",
    name: "fm",
    component: () => import("../views/fm/FM.vue"),
  },
  {
    path: "/videos",
    name: "videos",
    component: () => import("../views/videos/Videos.vue"),
    meta: {
      keepAlive: true,
    },
    children: [
      {
        path: "v",
        name: "videos.v",
        component: () => import("../views/videos/V.vue"),
        // component: () => import("../views/videos/V.jsx"),
      },
      {
        path: "mv",
        name: "videos.mv",
        component: () => import("../views/videos/MV.vue"),
      },
    ],
  },
  {
    path: "/played",
    name: "played",
    component: () => import("@/views/Played.vue"),
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/search/:keywords",
    name: "search",
    component: () => import("@/views/SearchResult.vue"),
    meta: {
      keepAlive: false,
      scrollTop: 0,
    },
    props: true,
  },
  {
    path: "/friends",
    name: "friends",
    component: () => import("../views/friends/Friends.vue"),
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  //发布router.beforeEach
  
  store.commit("saveSettings", {
    currentRoute: to.fullPath,
  });
  // const scrollTop = $content ? $content.scrollTop : 0;
  // from.meta.scrollTop = scrollTop;
  // }
  // to and from are both route objects. must call `next`.
  next();
});

// window.addEventListener(
//   "popstate",
//   (e) => {
//     console.log("popstate ", e);
//   },
//   false
// );

router.afterEach((to, from) => {
  // to and from are both route objects.

  saveBackSnaps() //保存返回设置

  
  //发布router.afterEach订阅
  // pubsub.publish('router.afterEach', {to: to, frmo: from})
  // eventBus.emit('router.afterEach', {to: to, frmo: from})
  // console.log('router----',to.path)
  store.commit("saveSettings", {
    currentRoute: to.fullPath,
  });
  if (to.meta.title) document.title = to.meta.title;
});

export default router;
