import Mock from 'mockjs';
import setupMock, { successResponseWrap } from '@/mocks/setup';

const haveReadIds: number[] = [];
const getMessageList = () => {
  return [
    {
      id: 1,
      type: 'message',
      title: '\u90d1\u6666\u6708',
      subTitle: '\u7684\u79c1\u4fe1',
      avatar:
        '//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/8361eeb82904210b4f55fab888fe8416.png~tplv-uwbnlip3yd-webp.webp',
      content: '\u5ba1\u6279\u8bf7\u6c42\u5df2\u53d1\u9001\uff0c\u8bf7\u67e5\u6536',
      time: '\u4eca\u5929 12:30:01',
    },
    {
      id: 2,
      type: 'message',
      title: '\u5b81\u6ce2',
      subTitle: '\u7684\u56de\u590d',
      avatar:
        '//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp',
      content: '\u6b64\u5904 bug \u5df2\u7ecf\u4fee\u590d',
      time: '\u4eca\u5929 12:30:01',
    },
    {
      id: 3,
      type: 'message',
      title: '\u5b81\u6ce2',
      subTitle: '\u7684\u56de\u590d',
      avatar:
        '//p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp',
      content: '\u6b64\u5904 bug \u5df2\u7ecf\u4fee\u590d',
      time: '\u4eca\u5929 12:20:01',
    },
    {
      id: 4,
      type: 'notice',
      title: '\u7eed\u8d39\u901a\u77e5',
      subTitle: '',
      avatar: '',
      content:
        '\u60a8\u7684\u4ea7\u54c1\u4f7f\u7528\u671f\u9650\u5373\u5c06\u622a\u6b62\uff0c\u5982\u9700\u7ee7\u7eed\u4f7f\u7528\u4ea7\u54c1\u8bf7\u524d\u5f80\u8d2d\u4e70',
      time: '\u4eca\u5929 12:20:01',
      messageType: 3,
    },
    {
      id: 5,
      type: 'notice',
      title: '\u89c4\u5219\u5f00\u901a\u6210\u529f',
      subTitle: '',
      avatar: '',
      content:
        '\u5185\u5bb9\u5c4f\u853d\u89c4\u5219\u4e8e 2021-12-01 \u5f00\u901a\u6210\u529f\u5e76\u751f\u6548',
      time: '\u4eca\u5929 12:20:01',
      messageType: 1,
    },
    {
      id: 6,
      type: 'todo',
      title: '\u8d28\u68c0\u961f\u5217\u53d8\u66f4',
      subTitle: '',
      avatar: '',
      content:
        '\u5185\u5bb9\u8d28\u68c0\u961f\u5217\u4e8e 2021-12-01 19:50:23 \u8fdb\u884c\u53d8\u66f4\uff0c\u8bf7\u91cd\u65b0\u63d0\u4ea4',
      time: '\u4eca\u5929 12:20:01',
      messageType: 0,
    },
  ].map((item) => ({
    ...item,
    status: haveReadIds.indexOf(item.id) === -1 ? 0 : 1,
  }));
};

setupMock({
  setup: () => {
    Mock.mock(/\/api\/message\/list/, () => {
      return successResponseWrap(getMessageList());
    });

    Mock.mock(/\/api\/message\/read/, (params: { body: string }) => {
      const { ids } = JSON.parse(params.body);
      haveReadIds.push(...(ids || []));
      return successResponseWrap(true);
    });
  },
});
