import { request } from './client'
import type {
  ChangeRequest,
  CreateChangeRequestBody,
  ReviewChangeRequestBody,
  MergeChangeRequestBody,
} from './types'

const enc = (v: string) => encodeURIComponent(v)

export const changeRequestsApi = {
  list: (wsID: string) =>
    request<ChangeRequest[]>(`/api/v1/workspaces/${enc(wsID)}/change-requests`),
  create: (wsID: string, body: CreateChangeRequestBody) =>
    request<ChangeRequest>(`/api/v1/workspaces/${enc(wsID)}/change-requests`, {
      method: 'POST',
      body,
    }),
  get: (id: string) => request<ChangeRequest>(`/api/v1/change-requests/${enc(id)}`),
  review: (id: string, body: ReviewChangeRequestBody) =>
    request<ChangeRequest>(`/api/v1/change-requests/${enc(id)}/review`, {
      method: 'POST',
      body,
    }),
  merge: (id: string, body?: MergeChangeRequestBody) =>
    request<ChangeRequest>(`/api/v1/change-requests/${enc(id)}/merge`, {
      method: 'POST',
      body: body ?? {},
    }),
  sync: (id: string) =>
    request<ChangeRequest>(`/api/v1/change-requests/${enc(id)}/sync`, { method: 'POST' }),
}
