common/helpers/cover-image.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getOpenLibraryCoverUrl = getOpenLibraryCoverUrl;
require("core-js/modules/es.array.find.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.string.includes.js");
require("core-js/modules/es.string.trim.js");
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.replace.js");
/*
 * Copyright (C) 2024  BookBrainz Contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * Gets the OpenLibrary cover image URL and source page URL for an Edition.
 * Priority: OLID > ISBN-13 > ISBN-10 > OCLC > LCCN
 * @param {(Array | null | undefined)} identifiers - Array of identifier objects with type and value
 * @param {('S' | 'M' | 'L')} size - Cover image size (S for small, M for medium, L for large)
 * @returns {(string[] | null)} [coverUrl, sourcePageUrl] or null if no identifier found
 */
function getOpenLibraryCoverUrl(identifiers) {
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'M';
  if (!identifiers || !Array.isArray(identifiers) || identifiers.length === 0) {
    return null;
  }
  var olidIdentifier = identifiers.find(function (identifier) {
    var _identifier$type, _identifier$type$labe, _identifier$type2;
    var typeLabel = ((_identifier$type = identifier.type) === null || _identifier$type === void 0 ? void 0 : (_identifier$type$labe = _identifier$type.label) === null || _identifier$type$labe === void 0 ? void 0 : _identifier$type$labe.toLowerCase()) || '';
    var typeId = (_identifier$type2 = identifier.type) === null || _identifier$type2 === void 0 ? void 0 : _identifier$type2.id;
    return typeId === 6 || typeLabel.includes('openlibrary') || typeLabel.includes('olid') || typeLabel.includes('ol id');
  });
  if (olidIdentifier && olidIdentifier.value) {
    var olidValue = olidIdentifier.value.trim();
    return ["https://covers.openlibrary.org/b/olid/".concat(olidValue, "-").concat(size, ".jpg?default=false"), "https://openlibrary.org/books/".concat(olidValue)];
  }
  var isbn13Identifier = identifiers.find(function (identifier) {
    var _identifier$type3, _identifier$type3$lab, _identifier$type4;
    var typeLabel = ((_identifier$type3 = identifier.type) === null || _identifier$type3 === void 0 ? void 0 : (_identifier$type3$lab = _identifier$type3.label) === null || _identifier$type3$lab === void 0 ? void 0 : _identifier$type3$lab.toLowerCase()) || '';
    var typeId = (_identifier$type4 = identifier.type) === null || _identifier$type4 === void 0 ? void 0 : _identifier$type4.id;
    return typeId === 9 || typeLabel === 'isbn-13';
  });
  if (isbn13Identifier && isbn13Identifier.value) {
    var cleanISBN = isbn13Identifier.value.replace(/-/g, '').trim();
    if (cleanISBN.length === 13) {
      return ["https://covers.openlibrary.org/b/isbn/".concat(cleanISBN, "-").concat(size, ".jpg?default=false"), "https://openlibrary.org/isbn/".concat(cleanISBN)];
    }
  }
  var isbn10Identifier = identifiers.find(function (identifier) {
    var _identifier$type5, _identifier$type5$lab, _identifier$type6;
    var typeLabel = ((_identifier$type5 = identifier.type) === null || _identifier$type5 === void 0 ? void 0 : (_identifier$type5$lab = _identifier$type5.label) === null || _identifier$type5$lab === void 0 ? void 0 : _identifier$type5$lab.toLowerCase()) || '';
    var typeId = (_identifier$type6 = identifier.type) === null || _identifier$type6 === void 0 ? void 0 : _identifier$type6.id;
    return typeId === 10 || typeLabel === 'isbn-10';
  });
  if (isbn10Identifier && isbn10Identifier.value) {
    var _cleanISBN = isbn10Identifier.value.replace(/-/g, '').trim();
    if (_cleanISBN.length === 10) {
      return ["https://covers.openlibrary.org/b/isbn/".concat(_cleanISBN, "-").concat(size, ".jpg?default=false"), "https://openlibrary.org/isbn/".concat(_cleanISBN)];
    }
  }
  var oclcIdentifier = identifiers.find(function (identifier) {
    var _identifier$type7, _identifier$type7$lab, _identifier$type8;
    var typeLabel = ((_identifier$type7 = identifier.type) === null || _identifier$type7 === void 0 ? void 0 : (_identifier$type7$lab = _identifier$type7.label) === null || _identifier$type7$lab === void 0 ? void 0 : _identifier$type7$lab.toLowerCase()) || '';
    var typeId = (_identifier$type8 = identifier.type) === null || _identifier$type8 === void 0 ? void 0 : _identifier$type8.id;
    return typeId === 26 || typeLabel.includes('oclc') || typeLabel.includes('ocn') || typeLabel.includes('worldcat');
  });
  if (oclcIdentifier && oclcIdentifier.value) {
    var oclcValue = oclcIdentifier.value.trim();
    if (/^\d+$/.test(oclcValue)) {
      return ["https://covers.openlibrary.org/b/oclc/".concat(oclcValue, "-").concat(size, ".jpg?default=false"), "https://openlibrary.org/oclc/".concat(oclcValue)];
    }
  }
  var lccnIdentifier = identifiers.find(function (identifier) {
    var _identifier$type9, _identifier$type9$lab, _identifier$type10;
    var typeLabel = ((_identifier$type9 = identifier.type) === null || _identifier$type9 === void 0 ? void 0 : (_identifier$type9$lab = _identifier$type9.label) === null || _identifier$type9$lab === void 0 ? void 0 : _identifier$type9$lab.toLowerCase()) || '';
    var typeId = (_identifier$type10 = identifier.type) === null || _identifier$type10 === void 0 ? void 0 : _identifier$type10.id;
    return typeId === 24 || typeLabel === 'lccn';
  });
  if (lccnIdentifier && lccnIdentifier.value) {
    var lccnValue = lccnIdentifier.value.trim();
    if (/^[a-zA-Z]{0,3}\d{6,10}$/.test(lccnValue)) {
      return ["https://covers.openlibrary.org/b/lccn/".concat(lccnValue, "-").concat(size, ".jpg?default=false"), "https://openlibrary.org/lccn/".concat(lccnValue)];
    }
  }
  return null;
}
//# sourceMappingURL=cover-image.js.map