import marked from 'marked';
import * as React from 'react';
import { Link } from 'react-router-dom';
import { Markdown } from '../../components/Markdown';
import { isErrorLike } from '../../util/errors';
import { ExtensionNoManifestAlert } from './RegistryExtensionManifestPage';
const PublishNewManifestAlert = ({ extension, text, buttonLabel, alertClass }) => (React.createElement("div", { className: `alert ${alertClass}` },
    text,
    extension.registryExtension &&
        extension.registryExtension.viewerCanAdminister && (React.createElement(React.Fragment, null,
        React.createElement("br", null),
        React.createElement(Link, { className: "mt-3 btn btn-primary", to: `${extension.registryExtension.url}/-/releases/new` }, buttonLabel)))));
export const ExtensionREADME = ({ extension }) => {
    if (!extension.rawManifest) {
        return React.createElement(ExtensionNoManifestAlert, { extension: extension });
    }
    const manifest = extension.manifest;
    if (isErrorLike(manifest)) {
        return (React.createElement(PublishNewManifestAlert, { extension: extension, alertClass: "alert-danger", text: `This extension's manifest is invalid: ${manifest && manifest.message ? manifest.message : 'JSON parse error'}`, buttonLabel: "Fix manifest and publish new release" }));
    }
    if (!manifest || !manifest.readme) {
        return (React.createElement(React.Fragment, null,
            manifest && manifest.title && React.createElement("h2", null, manifest.title),
            React.createElement(PublishNewManifestAlert, { extension: extension, alertClass: "alert-info", text: `This extension has no README.`, buttonLabel: "Add README and publish new release" })));
    }
    try {
        const html = marked(manifest.readme, { gfm: true, breaks: true, sanitize: true });
        return React.createElement(Markdown, { dangerousInnerHTML: html });
    }
    catch (err) {
        return (React.createElement(PublishNewManifestAlert, { extension: extension, alertClass: "alert-danger", text: "This extension's Markdown README is invalid.", buttonLabel: "Fix README and publish new release" }));
    }
};
//# sourceMappingURL=RegistryExtensionREADME.js.map