"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Audible = void 0;

var _axios = _interopRequireDefault(require("axios"));

var _metadataProvider = require("../metadataProvider");

var _globalSettings = require("../../repository/globalSettings");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class Audible extends _metadataProvider.MetadataProvider {
  name = 'audible';
  mediaType = 'audiobook';
  languages = {
    au: 'au',
    ca: 'ca',
    de: 'de',
    fr: 'fr',
    in: 'in',
    it: 'it',
    es: 'es',
    jp: 'co.jp',
    uk: 'co.uk',
    us: 'com'
  };

  countryCode() {
    var _GlobalConfiguration$;

    return (_GlobalConfiguration$ = _globalSettings.GlobalConfiguration.configuration.audibleLang) === null || _GlobalConfiguration$ === void 0 ? void 0 : _GlobalConfiguration$.toLocaleLowerCase();
  }

  domain(countryCode) {
    if (countryCode in this.languages) {
      return this.languages[countryCode];
    }

    return this.languages['us'];
  }

  queryParams = {
    response_groups: ['contributors', 'rating', 'media', 'product_attrs'].join(','),
    image_sizes: [500, 1000, 2400].join(',')
  };

  async search(query) {
    const countryCode = this.countryCode();
    const res = await _axios.default.get(`https://api.audible.${this.domain(countryCode)}/1.0/catalog/products`, {
      params: {
        title: query,
        num_results: 25,
        products_sort_by: 'Relevance',
        ...this.queryParams
      }
    });

    if (res.status === 200) {
      return res.data.products.map(product => this.mapResponse(product, countryCode));
    }

    throw new Error(`Error: ${res.status}`);
  }

  async details(arg) {
    var _res$data$product;

    const {
      audibleId
    } = arg;
    const countryCode = arg.countryCode || _globalSettings.GlobalConfiguration.configuration.audibleLang;
    const res = await _axios.default.get(`https://api.audible.${this.domain(countryCode)}/1.0/catalog/products/${audibleId + 1}`, {
      params: this.queryParams
    });

    if (res.status !== 200) {
      throw new Error(`Error: ${res.status}`);
    }

    if (((_res$data$product = res.data.product) === null || _res$data$product === void 0 ? void 0 : _res$data$product.title) === undefined) {
      throw new Error(`No metadata from Audible for ID ${audibleId}. Url: ${res.config.url}. Response: ${JSON.stringify(res.data, null, 2)}`);
    }

    return this.mapResponse(res.data.product, countryCode);
  }

  mapResponse(item, countryCode) {
    var _item$authors, _item$narrators, _item$product_images;

    return {
      needsDetails: false,
      mediaType: this.mediaType,
      source: this.name,
      audibleCountryCode: countryCode,
      title: item.title,
      audibleId: item.asin,
      authors: (_item$authors = item.authors) === null || _item$authors === void 0 ? void 0 : _item$authors.map(author => author.name),
      narrators: (_item$narrators = item.narrators) === null || _item$narrators === void 0 ? void 0 : _item$narrators.map(narrator => narrator.name),
      poster: (_item$product_images = item.product_images) === null || _item$product_images === void 0 ? void 0 : _item$product_images[2400],
      language: item.language,
      releaseDate: item.release_date,
      runtime: item.runtime_length_min,
      overview: item.merchandising_summary
    };
  }

}

exports.Audible = Audible;
let AudibleResponse;

(function (_AudibleResponse) {})(AudibleResponse || (AudibleResponse = {}));
//# sourceMappingURL=audible.js.map