
import { fetcher } from '@cloudron/pankow';
import { API_ORIGIN } from '../constants.js';

function create() {
  const accessToken = localStorage.token;

  return {
    async list(domain) {
      const perPage = 5000;

      let page = 1;
      let mailboxes = [];

      while (true) {
        let result;
        try {
          result = await fetcher.get(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes`, { page, per_page: perPage, access_token: accessToken });
        } catch (e) {
          return [e];
        }

        if (result.status !== 200) return [result];

        mailboxes = mailboxes.concat(result.body.mailboxes);

        if (result.body.mailboxes.length < perPage) break;

        page++;
      }

      return [null, mailboxes];
    },
    async get(domain, name) {
      let result;
      try {
        result = await fetcher.get(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, { access_token: accessToken });
      } catch (e) {
        return [e];
      }

      if (result.status !== 200) return [result];
      return [null, result.body.mailbox];
    },
    async add(domain, name, options) {
      const data = {
        name: name,
        ownerId: options.ownerId,
        ownerType: options.ownerType,
        active: !!options.active,
        enablePop3: !!options.enablePop3,
        storageQuota: options.storageQuota ||0,
        messagesQuota: options.messagesQuota || 0,
      };

      let result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes`, data, { access_token: accessToken });
      } catch (e) {
        return [e];
      }

      if (result.status !== 201) return [result];
      return [null];
    },
    async update(domain, name, options) {
      const data = {
        ownerId: options.ownerId,
        ownerType: options.ownerType,
        active: !!options.active,
        enablePop3: !!options.enablePop3,
        storageQuota: options.storageQuota ||0,
        messagesQuota: options.messagesQuota || 0,
      };

      let result;
      try {
        result = await fetcher.post(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, data, { access_token: accessToken });
      } catch (e) {
        return [e];
      }

      if (result.status !== 204) return [result];
      return [null];
    },
    async remove(domain, name, deleteMails = false) {
      let result;
      try {
        result = await fetcher.del(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}`, { deleteMails }, { access_token: accessToken });
      } catch (e) {
        return [e];
      }

      if (result.status !== 201) return [result];
      return [null];
    },
    async setAliases(domain, name, aliases) {
      let result;
      try {
        result = await fetcher.put(`${API_ORIGIN}/api/v1/mail/${domain}/mailboxes/${name}/aliases`, { aliases }, { access_token: accessToken });
      } catch (e) {
        return [e];
      }

      if (result.status !== 202) return [result];
      return [null];
    },
  };
}

export default {
  create,
};
