/* eslint-disable compat/compat */
// came.io
export default async function loginRemote(username, password) {
  const fetchData = await fetch(
    `https://sso.came.io/api/sso/account/login?Accept-Bc-version=v2`,
    {
      method: "POST",
      body: JSON.stringify({
        email: username,
        password,
      }),
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function getSites() {
  const fetchData = await fetch(`https://user.came.io/capi/sites`, {
    method: "GET",
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function getCounters(siteId) {
  const fetchData = await fetch(
    `https://${siteId.replaceAll("-", "")}.site.came.io/apiv1/counter/all`,
    {
      method: "GET",
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function getCounterById(siteId, counterId) {
  const fetchData = await fetch(
    `https://${siteId.replaceAll(
      "-",
      ""
    )}.site.came.io/apiv1/counter/${counterId}`,
    {
      method: "GET",
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function setCounterById(siteId, counterId, value, localName) {
  // Below should come from mainWindow
  // const localName = rootStore.counterList.getCounterById(counterId);
  if (typeof localName === "undefined") {
    return new Error();
  }
  const fetchData = await fetch(
    `https://${siteId.replaceAll("-", "")}.site.came.io/apiv1/counter/update`,
    {
      method: "PUT",
      body: JSON.stringify({
        id: counterId,
        name: localName.name,
        value,
      }),
    }
  )
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}

export async function logoutRemote() {
  const fetchData = await fetch(`https://sso.came.io/api/sso/account/logout`, {
    method: "POST",
  })
    .then((response) => {
      if (response.status !== 200) {
        return new Error(response.status);
      }
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => {
      return error;
    });
  return fetchData;
}
