import EyeIcon from 'mdi-react/EyeIcon';
import * as React from 'react';
import { ActionItem } from '../../../components/ActionItem';
import { Tooltip } from '../../../components/tooltip/Tooltip';
/**
 * A repository header action that toggles between showing a rendered file and the file's original
 * source, for files that can be rendered (such as Markdown files).
 */
export class ToggleRenderedFileMode extends React.PureComponent {
    /**
     * Reports whether the location's URL displays the blob as rendered or source.
     */
    static getModeFromURL(location) {
        const q = new URLSearchParams(location.search);
        if (!q.has(ToggleRenderedFileMode.URL_QUERY_PARAM)) {
            const isDiscussions = new URLSearchParams(location.hash).get('tab') === 'discussions';
            if (isDiscussions) {
                return 'code';
            }
            return undefined;
        }
        return q.get(ToggleRenderedFileMode.URL_QUERY_PARAM) === 'code' ? 'code' : 'rendered'; // default to rendered
    }
    /**
     * Returns the URL that displays the blob using the specified mode.
     */
    static getURLForMode(location, mode) {
        const q = new URLSearchParams(location.search);
        if (mode === 'code') {
            q.set(ToggleRenderedFileMode.URL_QUERY_PARAM, mode);
        }
        else {
            q.delete(ToggleRenderedFileMode.URL_QUERY_PARAM);
        }
        return { search: q.toString() };
    }
    componentDidUpdate(prevProps) {
        if (prevProps.mode !== this.props.mode) {
            Tooltip.forceUpdate();
        }
    }
    render() {
        const otherMode = this.props.mode === 'code' ? 'rendered' : 'code';
        return (React.createElement(ActionItem, { to: ToggleRenderedFileMode.getURLForMode(this.props.location, otherMode), "data-tooltip": otherMode === 'code' ? 'Show raw code file' : 'Show formatted file' },
            React.createElement(EyeIcon, { className: "icon-inline" }),
            ' ',
            React.createElement("span", { className: "d-none d-lg-inline" }, otherMode === 'code' ? 'Raw' : 'Formatted')));
    }
}
ToggleRenderedFileMode.URL_QUERY_PARAM = 'view';
//# sourceMappingURL=ToggleRenderedFileMode.js.map