import { http, HttpResponse } from 'msw';
import { roleMockData } from '../role.js';

export const roleHandlers = [
  // 获取角色列表
  http.post('*/role/list', async ({ request }) => {
    const data = await request.json();
    const result = roleMockData.getList(data);
    return HttpResponse.json(result);
  }),

  // 获取角色详情
  http.post('*/role/detail', async ({ request }) => {
    const data = await request.json();
    const result = roleMockData.getDetail(data.id);
    return HttpResponse.json(result);
  }),

  // 创建角色
  http.post('*/role/add', async ({ request }) => {
    const data = await request.json();
    const result = roleMockData.create(data);
    return HttpResponse.json(result);
  }),

  // 更新角色
  http.post('*/role/edit', async ({ request }) => {
    const data = await request.json();
    const result = roleMockData.update(data.id, data);
    return HttpResponse.json(result);
  }),

  // 删除角色
  http.post('*/role/delete', async ({ request }) => {
    const data = await request.json();
    const result = roleMockData.delete(data.id);
    return HttpResponse.json(result);
  }),

  // 获取角色选择列表（用于级联选择）
  http.post('*/role/select', async ({ request }) => {
    const selectData = roleMockData.list
      .filter((item) => item.status === 'normal')
      .map((item) => ({
        id: item.id,
        name: item.name,
        value: item.id,
        label: item.name,
      }));

    return HttpResponse.json({
      code: '200',
      message: '操作成功',
      data: selectData,
    });
  }),
];
