import { User, makeRequest } from "@frontend/kitui"
import { PatchUserRequest } from "@root/model/user"
import { parseAxiosError } from "@root/utils/parse-error"

const apiUrl = process.env.REACT_APP_DOMAIN + "/user"

export async function patchUser(
	user: PatchUserRequest
): Promise<[User | null, string?]> {
	try {
		const patchUserResponse = await makeRequest<PatchUserRequest, User>({
			url: apiUrl+"/",
			contentType: true,
			method: "PATCH",
			useToken: true,
			withCredentials: false,
			body: user,
		})

		return [patchUserResponse]
	} catch (nativeError) {
		const [error] = parseAxiosError(nativeError)

		return [null, `Не удалось изменить пользователя. ${error}`]
	}
}
