// Libs
// -----------------------------------------------------------------------------
import { makeAutoObservable, runInAction } from 'mobx';

// App
// -----------------------------------------------------------------------------
import request from 'services/request';
import { TypeLicense } from 'types/default';
import { environment } from 'enviroments/enviroments';

export class LicenseStore {
  constructor() {
    makeAutoObservable(this);
  }

  isLoading = false;

  isActivated: boolean | null = null;

  license: TypeLicense | null = null;

  setLicense(value: TypeLicense): void {
    this.license = value;
  }

  fetchLicense = async (): Promise<void> => {
    this.isLoading = true;
    try {
      const licenseResponse = await request<void, TypeLicense>({
        method: 'GET',
        url: environment.endPoints.license.info,
      });

      runInAction(() => {
        this.isActivated = true;
        this.setLicense(licenseResponse.data);
      });
    } catch {
      runInAction(() => {
        this.isActivated = false;
      });
    } finally {
      runInAction(() => {
        this.isLoading = false;
      });
    }
  };
}
