import Prismic from 'prismic-javascript';
import { ResourceType, ResourceSource } from '../models';
export const Client = (endpoint, req = null) => Prismic.client(endpoint, createClientOptions(req, null));
const createClientOptions = (req = null, prismicAccessToken = null) => {
    const reqOption = req ? { req } : {};
    const accessTokenOption = prismicAccessToken ? { accessToken: prismicAccessToken } : {};
    return Object.assign(Object.assign({}, reqOption), accessTokenOption);
};
export const prismicDocToResource = (doc) => {
    return {
        id: doc.uid,
        title: doc.data.title || null,
        description: doc.data.tagline || null,
        tags: doc.tags || [],
        publishDate: doc.first_publication_date || null,
        updatedDate: doc.last_publication_date || null,
        type: prismicTypeToResourceType(doc.type),
        authors: getAuthorsForPrismicDoc(doc),
        metaImage: getImage(doc.data.meta_image),
        heroImage: getImage(doc.data.hero_image || doc.data.cover_image),
        source: ResourceSource.Prismic,
        doc,
    };
};
const getImage = (imageObj) => (imageObj && imageObj.url ? imageObj.url : '');
export const getAuthorsForPrismicDoc = (doc) => {
    var _a, _b;
    if (!doc.data.hosts && !doc.data.author) {
        return [];
    }
    if (doc.type === 'webinar') {
        return doc.data.hosts.map((h) => {
            var _a, _b;
            return ({
                name: h.name || '',
                title: h.title || '',
                link: ((_a = h.profile_link) === null || _a === void 0 ? void 0 : _a.url) || '',
                avatar: ((_b = h.photo) === null || _b === void 0 ? void 0 : _b.url) || '',
            });
        });
    }
    else if (doc.data.author && doc.data.author.length) {
        return doc.data.author.map((a) => {
            var _a, _b;
            return ({
                name: a.name || '',
                title: a.title || '',
                link: ((_a = a.author_url) === null || _a === void 0 ? void 0 : _a.url) || '',
                avatar: ((_b = a.photo) === null || _b === void 0 ? void 0 : _b.url) || '',
            });
        });
    }
    else if (doc.data.author) {
        return [
            {
                name: doc.data.author.name || '',
                title: doc.data.author.title || '',
                link: ((_a = doc.data.author.author_url) === null || _a === void 0 ? void 0 : _a.url) || '',
                avatar: ((_b = doc.data.author.photo) === null || _b === void 0 ? void 0 : _b.url) || '',
            },
        ];
    }
    return [];
};
export const prismicTypeToResourceType = (type) => ({
    article: ResourceType.Article,
    book: ResourceType.Book,
    blog: ResourceType.Blog,
    case_study: ResourceType.CaseStudy,
    course: ResourceType.Course,
    customer_story: ResourceType.CustomerInterview,
    learning: ResourceType.Learning,
    doc: ResourceType.Doc,
    guide: ResourceType.Guide,
    podcast: ResourceType.Podcast,
    tutorial: ResourceType.Tutorial,
    video: ResourceType.Video,
    webinar: ResourceType.Webinar,
    whitepaper: ResourceType.Whitepaper,
}[type]);
export const resourceTypeToPrismicType = (type) => ({
    [ResourceType.Article]: 'article',
    [ResourceType.Blog]: 'blog',
    [ResourceType.Book]: 'book',
    [ResourceType.CaseStudy]: 'case_study',
    [ResourceType.Course]: 'course',
    [ResourceType.CustomerInterview]: 'customer_story',
    [ResourceType.Doc]: 'doc',
    [ResourceType.Learning]: 'learning',
    [ResourceType.Guide]: 'guide',
    [ResourceType.Podcast]: 'podcast',
    [ResourceType.Tutorial]: 'tutorial',
    [ResourceType.Video]: 'video',
    [ResourceType.Webinar]: 'webinar',
    [ResourceType.Whitepaper]: 'whitepaper',
}[type]);
export function linkResolver(doc) {
    // Define the url depending on the document type
    if (doc.type === 'article') {
        return '/resources/articles/' + doc.uid;
    }
    else if (doc.type === 'case_study') {
        return '/resources/case-studies/' + doc.uid;
    }
    else if (doc.type === 'customer_story') {
        return '/resources/customer-interviews/' + doc.uid;
    }
    else if (doc.type === 'enterprise_blog_post') {
        return '/enterprise/blog/' + doc.uid;
    }
    else if (doc.type === 'integration') {
        return '/integrations/' + doc.uid;
    }
    else if (doc.type === 'podcast') {
        return '/resources/podcasts/' + doc.uid;
    }
    else if (doc.type === 'thank_you') {
        return '/thank-you/' + doc.uid;
    }
    else if (doc.type === 'video') {
        return '/resources/videos/' + doc.uid;
    }
    else if (doc.type === 'webinar') {
        return '/resources/webinars/' + doc.uid;
    }
    else if (doc.type === 'whitepaper') {
        return '/resources/whitepapers/' + doc.uid;
    }
    // Default to homepage
    return '/';
}
export const resourceTypeToPath = (type) => ({
    [ResourceType.Article]: 'articles',
    [ResourceType.Blog]: 'blogs',
    [ResourceType.Book]: 'blogs',
    [ResourceType.CaseStudy]: 'case-studies',
    [ResourceType.Course]: 'courses',
    [ResourceType.CustomerInterview]: 'customer-interviews',
    [ResourceType.Doc]: 'docs',
    [ResourceType.Learning]: 'learning',
    [ResourceType.Guide]: 'guides',
    [ResourceType.Podcast]: 'podcasts',
    [ResourceType.Tutorial]: 'tutorials',
    [ResourceType.Video]: 'videos',
    [ResourceType.Webinar]: 'webinars',
    [ResourceType.Whitepaper]: 'whitepapers',
}[type]);
