const html = require('choo/html');
const { reviewCard } = require('../partials/reviewCard');
const { starRating } = require('../partials/starRating');
const { modal } = require('../partials/modal');
const resultDetails = (searchController, result, emit = () => {}) => {
const { __ } = searchController.i18n;
const source = result.sources[0];
const modalId = `result_${source.uri}`;
const hasReviews = typeof result.averageRating !== 'undefined' && typeof result.numberOfReviews !== 'undefined';
const buttonHTML = html``;
const coversHTMLArray = result.covers.map((cover, index, allCovers) => {
return html`
${typeof allCovers[index - 1] === 'undefined'
? null
: html`
`
}
${typeof allCovers[index + 1] === 'undefined'
? null
: html`
`
}
`;
});
const modalContent = html`
${__('search.covers')}
${typeof result.covers === 'undefined'
? html`
`
: html`
${result.covers.map((cover, index) => {
return [
html`
`,
// html`
`,
];
})}
${
searchController.openModal === modalId
? coversHTMLArray
: '' /* Leave the covers column empty until opened to prevent loading too many images */
}
`
}
${!hasReviews
? html`
${__('search.no_reviews')}
`
: html`
${__('interaction.average_rating')}
${starRating(result.averageRating)}
${(typeof result.reviews !== 'undefined' && Array.isArray(result.reviews) ? result.reviews : []).map(review => {
return reviewCard(searchController, review);
})}`
}
${!searchController.showShelves ? null : html`
${searchController.shelves.map(shelf => {
return html`-
`;
})}
`}
${__('search.see_book_details')}
`;
const onShow = () => {
const coversColumn = document.getElementById(`covers_${modalId}`);
coversColumn.innerHTML = '';
coversHTMLArray.forEach(element => coversColumn.appendChild(element));
};
return modal(modalId, searchController, modalContent, {
styles: "width:90%;",
buttonHTML, // This should be replaced with buttonHTML containing the ratings and number of reviews etc.
headerText: result.name,
onShow,
});
}
module.exports = { resultDetails };