/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * 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.
 * ~~~~~~/licensing~~~~~~
 */
var Entaxy;
(function (Entaxy) {

    function convertToHtmlInputType(javaType) {
        if (!javaType || javaType.startsWith('entaxy.runtime')) {
            // fixme maybe
            return javaType;
        }
        switch (javaType) {
            case 'boolean':
            case 'Boolean':
            case 'java.lang.Boolean':
                return 'checkbox';
            case 'int':
            case 'long':
            case 'java.lang.Integer':
            case 'java.lang.Long':
                return 'number';
            default:
                return 'text';
        }
    }
    Entaxy.convertToHtmlInputType = convertToHtmlInputType;

    function notification(type, message, duration) {

        let visibleToastElem = null;
        let timeoutId;

        let toastHtml = `
            <div class="toast-pf alert alert-${type} alert-dismissable">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">
                    <span class="pficon pficon-close"></span>
                </button>
                <span class="pficon pficon-${resolveToastIcon(type)}"></span>
                ${message}
            </div>
        `;

        let toastFrag = document.createRange().createContextualFragment(toastHtml);
        let toastElem = toastFrag.querySelector('div');

        let bodyElem = document.querySelector('body');

        // if toast element is in the DOM
        if (visibleToastElem && visibleToastElem.parentNode) {
            clearTimeout(timeoutId);
            bodyElem.removeChild(visibleToastElem);
        }
        visibleToastElem = bodyElem.appendChild(toastElem);

        if (duration || (type === 'success' || type === 'info')) {
            timeoutId = window.setTimeout(() => {
                if (toastElem.parentNode) {
                    bodyElem.removeChild(toastElem);
                }
            }, duration || 8000);
        }
    }
    Entaxy.notification = notification;

    function resolveToastIcon(type) {
        switch (type) {
            case 'success':
                return 'ok';
            case 'warning':
                return 'warning-triangle-o';
            case 'danger':
                return 'error-circle-o';
            default:
                return 'info';
        }
    }

})(Entaxy || (Entaxy = {}));
