// Copyright (C) 2017-2020 Smart code 203358507
const React = require('react');
const PropTypes = require('prop-types');
const classnames = require('classnames');
const UrlUtils = require('url');
const Icon = require('stremio-icons/dom');
const Button = require('stremio/common/Button');
const Image = require('stremio/common/Image');
const ModalDialog = require('stremio/common/ModalDialog');
const SharePrompt = require('stremio/common/SharePrompt');
const CONSTANTS = require('stremio/common/CONSTANTS');
const deepLinking = require('stremio/common/deepLinking');
const routesRegexp = require('stremio/common/routesRegexp');
const useBinaryState = require('stremio/common/useBinaryState');
const ActionButton = require('./ActionButton');
const MetaLinks = require('./MetaLinks');
const MetaPreviewPlaceholder = require('./MetaPreviewPlaceholder');
const styles = require('./styles');
const ALLOWED_LINK_REDIRECTS = [
routesRegexp.search.regexp,
routesRegexp.discover.regexp,
routesRegexp.metadetails.regexp
];
const MetaPreview = ({ className, compact, name, logo, background, runtime, releaseInfo, released, description, links, trailer, inLibrary, toggleInLibrary }) => {
const [shareModalOpen, openShareModal, closeShareModal] = useBinaryState(false);
const linksGroups = React.useMemo(() => {
return links
.filter((link) => link && typeof link.category === 'string' && typeof link.url === 'string')
.reduce((linksGroups, { category, name, url }) => {
if (category === CONSTANTS.IMDB_LINK_CATEGORY) {
linksGroups.set(category, {
label: name,
href: `https://www.stremio.com/warning#${encodeURIComponent(`https://www.imdb.com/title/${encodeURIComponent(url)}`)}`
});
} else if (category === CONSTANTS.SHARE_LINK_CATEGORY) {
linksGroups.set(category, {
label: name,
href: url
});
} else {
const { protocol, host, path, pathname } = UrlUtils.parse(url);
if (protocol === 'stremio:') {
if (pathname !== null && ALLOWED_LINK_REDIRECTS.some((regexp) => pathname.match(regexp))) {
if (!linksGroups.has(category)) {
linksGroups.set(category, []);
}
linksGroups.get(category).push({
label: name,
href: `#${path}`
});
}
} else if (typeof host === 'string' && host.length > 0) {
if (!linksGroups.has(category)) {
linksGroups.set(category, []);
}
linksGroups.get(category).push({
label: name,
href: `https://www.stremio.com/warning#${encodeURIComponent(url)}`
});
}
}
return linksGroups;
}, new Map());
}, [links]);
const trailerHref = React.useMemo(() => {
if (typeof trailer !== 'object' || trailer === null) {
return null;
}
const deepLinks = deepLinking.withStream({ stream: trailer });
return deepLinks.player;
}, [trailer]);
const renderLogoFallback = React.useMemo(() => () => (