/*
 * 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 { __extends } from "tslib";
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";
var 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 var DISPLAY_ALIASES = {
    ArrowDown: "down",
    ArrowLeft: "left",
    ArrowRight: "right",
    ArrowUp: "up",
};
var KeyComboTagInternal = /** @class */ (function (_super) {
    __extends(KeyComboTagInternal, _super);
    function KeyComboTagInternal() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.renderKey = function (key, index) {
            var _a;
            var _b;
            var keyString = (_b = DISPLAY_ALIASES[key]) !== null && _b !== void 0 ? _b : key;
            var icon = _this.getKeyIcon(key);
            var reactKey = "key-".concat(index);
            return (React.createElement("kbd", { className: classNames(Classes.KEY, (_a = {}, _a[Classes.MODIFIER_KEY] = icon != null, _a)), key: reactKey },
                icon != null && React.createElement(Icon, { icon: icon.icon, title: icon.iconTitle }),
                keyString));
        };
        _this.renderMinimalKey = function (key, index, isLastKey) {
            var icon = _this.getKeyIcon(key);
            if (icon == null) {
                return isLastKey ? key : React.createElement(React.Fragment, { key: "key-".concat(index) },
                    key,
                    "\u00A0+\u00A0");
            }
            return React.createElement(Icon, { icon: icon.icon, title: icon.iconTitle, key: "key-".concat(index) });
        };
        return _this;
    }
    KeyComboTagInternal.prototype.render = function () {
        var _a;
        var _this = this;
        var _b = this.props, className = _b.className, combo = _b.combo, minimal = _b.minimal, platformOverride = _b.platformOverride;
        var normalizedKeys = normalizeKeyCombo(combo, platformOverride);
        var keys = normalizedKeys
            .map(function (key) { return (key.length === 1 ? key.toUpperCase() : key); })
            .map(function (key, index) {
            return minimal
                ? _this.renderMinimalKey(key, index, index === normalizedKeys.length - 1)
                : _this.renderKey(key, index);
        });
        return React.createElement("span", { className: classNames(Classes.KEY_COMBO, className, (_a = {}, _a[Classes.MINIMAL] = minimal, _a)) }, keys);
    };
    KeyComboTagInternal.prototype.getKeyIcon = function (key) {
        var platformOverride = this.props.platformOverride;
        var icon = KEY_ICONS[key];
        if ((icon === null || icon === void 0 ? void 0 : icon.isMacOnly) && !isMac(platformOverride)) {
            return undefined;
        }
        return icon;
    };
    KeyComboTagInternal.displayName = "".concat(DISPLAYNAME_PREFIX, ".KeyComboTag");
    return KeyComboTagInternal;
}(AbstractPureComponent));
export { KeyComboTagInternal };
export var KeyComboTag = KeyComboTagInternal;
//# sourceMappingURL=keyComboTag.js.map