"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchOrganization = fetchOrganization;
exports.portalToSearchResult = portalToSearchResult;
exports.portalToOrganization = portalToOrganization;
const fetch_org_1 = require("./fetch-org");
const get_org_thumbnail_url_1 = require("../resources/get-org-thumbnail-url");
/**
 * Fetch an organization from the portal
 * Note: IHubOrganization is a read-only interface for a subset of the IPortal object
 * The full portal is exposed as the `portal` property on the IHubOrganization object.
 * @param identifier
 * @param requestOptions
 * @returns
 */
async function fetchOrganization(identifier, requestOptions) {
    const portal = await (0, fetch_org_1.fetchOrg)(identifier, requestOptions);
    const session = requestOptions.authentication;
    const token = session.token;
    return portalToOrganization(portal, token);
}
/**
 * Convert an IPortal to an IHubSearchResult
 * @param portal
 * @returns
 */
function portalToSearchResult(portal) {
    const org = portalToOrganization(portal);
    // simple transform
    return {
        id: org.id,
        title: org.name,
        name: org.name,
        url: org.url,
        type: "Organization",
        family: "organization",
        source: "portal",
        createdDate: org.createdDate,
        createdDateSource: org.createdDateSource,
        updatedDate: org.updatedDate,
        updatedDateSource: org.updatedDateSource,
        thumbnailUrl: org.thumbnailUrl,
        thumbnail: org.thumbnail,
        description: org.description,
        access: org.access,
        tags: org.tags,
        typeKeywords: org.typeKeywords,
        links: org.links,
    };
}
/**
 * Convert an IPortal to an IHubOrganization
 * @param portal
 * @param token
 * @returns
 */
function portalToOrganization(portal, token) {
    // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
    let portalUrl = `https://${portal.portalHostname}`;
    if (portal.urlKey) {
        // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
        portalUrl = `https://${portal.urlKey}.${portal.customBaseUrl}`;
    }
    return {
        id: portal.id,
        orgId: portal.id,
        access: portal.access,
        name: portal.name,
        summary: portal.description,
        description: portal.description,
        type: "Organization",
        source: "portal",
        createdDate: new Date(portal.created),
        createdDateSource: "portal",
        updatedDate: new Date(portal.modified),
        updatedDateSource: "portal",
        portal,
        thumbnail: portal.thumbnail,
        thumbnailUrl: (0, get_org_thumbnail_url_1.getOrgThumbnailUrl)(portal, token),
        url: portalUrl,
        links: {
            self: portalUrl,
            thumbnail: (0, get_org_thumbnail_url_1.getOrgThumbnailUrl)(portal, token),
        },
        // Props needed to be a HubEntity, but which don't apply to a portal
        typeKeywords: [],
        tags: [],
    };
}
//# sourceMappingURL=fetch.js.map