/*
 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import classNames from "classnames";
import * as React from "react";
import { ArrowDown, ArrowLeft, ArrowRight, ArrowUp, KeyCommand, KeyControl, KeyDelete, KeyEnter, KeyOption, KeyShift, } from "@blueprintjs/icons";
import { AbstractPureComponent, Classes, DISPLAYNAME_PREFIX } from "../../common";
import { Icon } from "../icon/icon";
import { isMac, normalizeKeyCombo } from "./hotkeyParser";
const KEY_ICONS = {
    alt: { icon: React.createElement(KeyOption, null), iconTitle: "Alt/Option key", isMacOnly: true },
    arrowdown: { icon: React.createElement(ArrowDown, null), iconTitle: "Down key" },
    arrowleft: { icon: React.createElement(ArrowLeft, null), iconTitle: "Left key" },
    arrowright: { icon: React.createElement(ArrowRight, null), iconTitle: "Right key" },
    arrowup: { icon: React.createElement(ArrowUp, null), iconTitle: "Up key" },
    cmd: { icon: React.createElement(KeyCommand, null), iconTitle: "Command key", isMacOnly: true },
    ctrl: { icon: React.createElement(KeyControl, null), iconTitle: "Control key", isMacOnly: true },
    delete: { icon: React.createElement(KeyDelete, null), iconTitle: "Delete key" },
    enter: { icon: React.createElement(KeyEnter, null), iconTitle: "Enter key" },
    meta: { icon: React.createElement(KeyCommand, null), iconTitle: "Command key", isMacOnly: true },
    shift: { icon: React.createElement(KeyShift, null), iconTitle: "Shift key", isMacOnly: true },
};
/** Reverse table of some CONFIG_ALIASES fields, for display by KeyComboTag */
export const DISPLAY_ALIASES = {
    ArrowDown: "down",
    ArrowLeft: "left",
    ArrowRight: "right",
    ArrowUp: "up",
};
export class KeyComboTagInternal extends AbstractPureComponent {
    static displayName = `${DISPLAYNAME_PREFIX}.KeyComboTag`;
    render() {
        const { className, combo, minimal, platformOverride } = this.props;
        const normalizedKeys = normalizeKeyCombo(combo, platformOverride);
        const keys = normalizedKeys
            .map(key => (key.length === 1 ? key.toUpperCase() : key))
            .map((key, index) => minimal
            ? this.renderMinimalKey(key, index, index === normalizedKeys.length - 1)
            : this.renderKey(key, index));
        return React.createElement("span", { className: classNames(Classes.KEY_COMBO, className, { [Classes.MINIMAL]: minimal }) }, keys);
    }
    renderKey = (key, index) => {
        const keyString = DISPLAY_ALIASES[key] ?? key;
        const icon = this.getKeyIcon(key);
        const reactKey = `key-${index}`;
        return (React.createElement("kbd", { className: classNames(Classes.KEY, { [Classes.MODIFIER_KEY]: icon != null }), key: reactKey },
            icon != null && React.createElement(Icon, { icon: icon.icon, title: icon.iconTitle }),
            keyString));
    };
    renderMinimalKey = (key, index, isLastKey) => {
        const icon = this.getKeyIcon(key);
        if (icon == null) {
            return isLastKey ? key : React.createElement(React.Fragment, { key: `key-${index}` },
                key,
                "\u00A0+\u00A0");
        }
        return React.createElement(Icon, { icon: icon.icon, title: icon.iconTitle, key: `key-${index}` });
    };
    getKeyIcon(key) {
        const { platformOverride } = this.props;
        const icon = KEY_ICONS[key];
        if (icon?.isMacOnly && !isMac(platformOverride)) {
            return undefined;
        }
        return icon;
    }
}
export const KeyComboTag = KeyComboTagInternal;
//# sourceMappingURL=keyComboTag.js.map