"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.UITranslationButton = void 0;
const react_1 = __importStar(require("react"));
const UIButton_1 = require("../UIButton");
const UICallout_1 = require("../UICallout");
const Icons_1 = require("../Icons");
const UITranslationButton_types_1 = require("./UITranslationButton.types");
const UILoadButton_1 = require("./UILoadButton");
require("./UITranslationInput.scss");
/**
 * Method to resolve button component text strings for passed key.
 * Component has default texts which can be overwritten using property `strings`.
 *
 * @param property Property.
 * @param strings Map with all text properties.
 * @returns Resolved text.
 */
const getStringText = (property, strings) => {
    return strings?.[property] ?? '';
};
/**
 * Component to render translation button to provide helper callout with i18n generation option.
 *
 * @param props Component properties.
 * @returns Component to render translation button with callout.
 */
const UITranslationButton = (props) => {
    const { id, strings, value, onCreateNewEntry, onUpdateValue, onShowExistingEntry, busy, suggestion } = props;
    const [calloutVisible, setCalloutVisible] = (0, react_1.useState)(false);
    // Callbacks
    const onToggleCallout = (0, react_1.useCallback)(() => {
        if (suggestion.suggest?.type === UITranslationButton_types_1.SuggestValueType.Existing) {
            setCalloutVisible(false);
            // Trigger show existing entry callbACK
            onShowExistingEntry?.(suggestion.suggest.entry);
        }
        else {
            setCalloutVisible(!calloutVisible);
        }
    }, [calloutVisible, suggestion]);
    const onAccept = (0, react_1.useCallback)(() => {
        if (suggestion.suggest) {
            if (suggestion.suggest.type === UITranslationButton_types_1.SuggestValueType.New) {
                onCreateNewEntry?.(suggestion.suggest.entry);
            }
            if (value !== suggestion.suggest.i18n) {
                onUpdateValue?.(suggestion.suggest.i18n ?? '');
            }
        }
        setCalloutVisible(false);
    }, [suggestion, onCreateNewEntry, onUpdateValue, value]);
    const onCancel = (0, react_1.useCallback)(() => {
        setCalloutVisible(false);
    }, []);
    return (react_1.default.createElement("div", { className: "ui-translatable__button" },
        react_1.default.createElement(UILoadButton_1.UILoadButton, { id: id, disabled: props.disabled ?? false, onClick: onToggleCallout, iconProps: { iconName: suggestion.suggest?.icon ?? Icons_1.UiIcons.World }, title: suggestion.tooltip, busy: busy?.busy, useMinWaitingTime: busy?.useMinWaitingTime }),
        calloutVisible && (react_1.default.createElement(UICallout_1.UICallout, { target: `#${id}`, gapSpace: 5, directionalHint: 5, calloutWidth: 250, calloutMinWidth: 250, beakWidth: 8, isBeakVisible: false, setInitialFocus: true, className: "ui-translatable__callout", onDismiss: () => onToggleCallout(), contentPadding: UICallout_1.UICalloutContentPadding.Standard },
            react_1.default.createElement("div", { className: "ui-translatable__message" },
                suggestion.message,
                react_1.default.createElement("div", { className: "ui-translatable__actions" },
                    react_1.default.createElement(UIButton_1.UIDefaultButton, { id: `${id}-button-action-confirm`, primary: true, onClick: onAccept }, getStringText('acceptButtonLabel', strings)),
                    react_1.default.createElement(UIButton_1.UIDefaultButton, { id: `${id}-button-action-cancel`, onClick: onCancel }, getStringText('cancelButtonLabel', strings))))))));
};
exports.UITranslationButton = UITranslationButton;
//# sourceMappingURL=UITranslationButton.js.map