import dayjs from "dayjs";

export const courseTimeList = [
  {
    id: 1,
    title: "这个非常OK啊1",
    time: "1661990400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 2,
    title: "这个非常OK啊2",
    time: "1630454400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 3,
    title: "这个非常OK啊333",
    time: "1625097600000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 4,
    title: "这个非常OK啊444",
    time: "1625284000000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 5,
    title: "这个非常OK啊3",
    time: "1598938400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 6,
    title: "这个非常OK啊3",
    time: "1528948400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 7,
    title: "这个非常OK啊3",
    time: "1538958400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 8,
    title: "这个非常OK啊3",
    time: "1538958400100",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 9,
    title: "这个非常OK啊3",
    time: "1538958400200",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 10,
    title: "这个非常OK啊3",
    time: "1538958400400",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 11,
    title: "这个非常OK啊3",
    time: "1538958400500",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 12,
    title: "这个非常OK啊3",
    time: "1538958400600",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 13,
    title: "这个非常OK啊3",
    time: "1591918400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 15,
    title: "这个非常OK啊3",
    time: "1592918400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
  {
    id: 16,
    title: "这个非常OK啊3",
    time: "1543918400000",
    img: "https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/a8c8cdb109cb051163646151a4a5083b.png~tplv-uwbnlip3yd-webp.webp",
  },
];

const process = (before: any[]) => {
  const after: any = {};
  // 提取月份,塞入数据
  before.forEach((item) => {
    const year = dayjs(+item.time).year();
    const month = dayjs(+item.time).month() + 1;
    if (!(year in after)) after[year] = { [month]: [] };
    if (!(month in after[year])) after[year][month] = [];
    after[year][month].push(item);
  });
  // 年月为key，倒叙排列
  const compare = (key: string) => (a: any, b: any) => b[key] - a[key];
  const ymArray = Object.keys(after)
    .reverse()
    .map((year) => {
      return Object.keys(after[year]).map((month) => ({
        year,
        month,
        index: +`${year}.${+month >= 10 ? month : "0" + month}`,
        data: after[year][month],
      }));
    })
    .flat()
    .sort(compare("index"));

  return ymArray;
};

export const courseTimeListDefault = process(courseTimeList);
