class JibestreamApi {
  async getToken(credentials) {
    const details = {
      client_id: credentials.clientId,
      client_secret: credentials.clientSecret,
      grant_type: 'client_credentials'
    };
    var formBody = [];
    for (var property in details) {
      var encodedKey = encodeURIComponent(property);
      var encodedValue = encodeURIComponent(details[property]);
      formBody.push(encodedKey + '=' + encodedValue);
    }
    const body = formBody.join('&');
    const options = {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      body: body
    };
    try {
      return await fetch(`${credentials.host}/JACS/api/auth/token`, options).then(async response => {
        if (!response.ok) {
          console.error('Request failed with status code ' + response.status + ': ' + response.statusText);
          if (response.status === 401) {
            console.error('Authentication failed for getToken');
          } else if (response.status === 403) {
            console.error('Authorization failed for getToken');
          } else {
            console.log('Error in getToken, with status code ' + response.status);
          }
          return undefined;
        }
        const resp = await response.json();
        return resp.access_token;
      }).catch(error => {
        console.error('Fetch request failed. Error: ' + error.message);
        return undefined;
      });
    } catch (e) {
      console.error('Error completing getToken' + e);
      return undefined;
    }
  }
  async getAllDestinations(credentials, venueIds) {
    const token = await this.getToken(credentials);
    console.log('GETTING DESTINATIONS', credentials, venueIds, token);
    if (!token) {
      return {};
    }
    const destinations = {};
    await Promise.all(venueIds.map(async venueId => {
      var _await$this$getVenueP;
      const destinationsCurr = (_await$this$getVenueP = await this.getVenueProperties(token, {
        ...credentials,
        venueId
      })) === null || _await$this$getVenueP === void 0 ? void 0 : _await$this$getVenueP.destinations.items;
      if (destinationsCurr !== undefined) destinations[venueId] = destinationsCurr;
      return null;
    }));
    return destinations;
  }
  async getVenueProperties(token, credentials) {
    const options = {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${token}`
      }
    };
    const queryParams = credentials.locale ? `?locale=${credentials.locale}` : '';
    try {
      return await fetch(`${credentials.host}/JACS/api/customer/${credentials.customerId}/venue/${credentials.venueId}/full${queryParams}`, options).then(response => {
        if (!response.ok) {
          console.error('Request failed with status code ' + response.status + ': ' + response.statusText);
          if (response.status === 401) {
            console.error('Authentication failed for getVenueProperties');
          } else if (response.status === 403) {
            console.error('Authorization failed for getVenueProperties');
          } else {
            console.log('Error in getVenueProperties, with status code ' + response.status);
          }
          return undefined;
        }
        const resp = response.json();
        return resp;
      }).catch(error => {
        console.error('Fetch request failed. Error: ' + error.message);
        return undefined;
      });
    } catch (e) {
      console.error('Error completing getVenueProperties' + e);
      return undefined;
    }
  }
}
export const jibestreamApi = new JibestreamApi();
//# sourceMappingURL=jibestreamApi.js.map