/**
 * DevExtreme (renovation/button.p.js)
 * Version: 20.2.0 (build 20182-0310)
 * Build date: Tue Jun 30 2020
 *
 * Copyright (c) 2012 - 2020 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";

function _typeof(obj) {
    "@babel/helpers - typeof";
    if ("function" === typeof Symbol && "symbol" === typeof Symbol.iterator) {
        _typeof = function(obj) {
            return typeof obj
        }
    } else {
        _typeof = function(obj) {
            return obj && "function" === typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
        }
    }
    return _typeof(obj)
}
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.defaultOptionRules = exports.ButtonProps = exports.viewFunction = void 0;
exports.defaultOptions = defaultOptions;
var _utils = require("../core/options/utils");
var _devices = require("../core/devices");
var _devices2 = _interopRequireDefault(_devices);
var _noop = require("./utils/noop");
var _noop2 = _interopRequireDefault(_noop);
var _themes = require("../ui/themes");
var themes = _interopRequireWildcard(_themes);
var _short = require("../events/short");
var _icon = require("../core/utils/icon");
var _icon2 = require("./icon.p");
var _icon3 = _interopRequireDefault(_icon2);
var _inkRipple = require("./ink-ripple.p");
var _inkRipple2 = _interopRequireDefault(_inkRipple);
var _widget = require("./widget.p");
var _widget2 = _interopRequireDefault(_widget);
var _baseProps = require("./utils/base-props.p");
var _baseProps2 = _interopRequireDefault(_baseProps);
var _preact = require("preact");
var Preact = _interopRequireWildcard(_preact);
var _hooks = require("preact/hooks");
var _compat = require("preact/compat");

function _getRequireWildcardCache() {
    if ("function" !== typeof WeakMap) {
        return null
    }
    var cache = new WeakMap;
    _getRequireWildcardCache = function() {
        return cache
    };
    return cache
}

function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj
    }
    if (null === obj || "object" !== _typeof(obj) && "function" !== typeof obj) {
        return {
            "default": obj
        }
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj)
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc)
            } else {
                newObj[key] = obj[key]
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj)
    }
    return newObj
}

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        "default": obj
    }
}

function _objectWithoutProperties(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = _objectWithoutPropertiesLoose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for (i = 0; i < sourceSymbolKeys.length; i++) {
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) {
                continue
            }
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) {
                continue
            }
            target[key] = source[key]
        }
    }
    return target
}

function _objectWithoutPropertiesLoose(source, excluded) {
    if (null == source) {
        return {}
    }
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) {
            continue
        }
        target[key] = source[key]
    }
    return target
}

function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable
            })
        }
        keys.push.apply(keys, symbols)
    }
    return keys
}

function _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = null != arguments[i] ? arguments[i] : {};
        if (i % 2) {
            ownKeys(Object(source), true).forEach(function(key) {
                _defineProperty(target, key, source[key])
            })
        } else {
            if (Object.getOwnPropertyDescriptors) {
                Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
            } else {
                ownKeys(Object(source)).forEach(function(key) {
                    Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
                })
            }
        }
    }
    return target
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        })
    } else {
        obj[key] = value
    }
    return obj
}

function _extends() {
    _extends = Object.assign || function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}
var stylingModes = ["outlined", "text", "contained"];
var getCssClasses = function(model) {
    var icon = model.icon,
        iconPosition = model.iconPosition,
        stylingMode = model.stylingMode,
        text = model.text,
        type = model.type;
    var classNames = ["dx-button"];
    var isValidStylingMode = stylingMode && stylingModes.indexOf(stylingMode) !== -1;
    classNames.push("dx-button-mode-".concat(isValidStylingMode ? stylingMode : "contained"));
    classNames.push("dx-button-".concat(type || "normal"));
    text && classNames.push("dx-button-has-text");
    icon && classNames.push("dx-button-has-icon");
    "left" !== iconPosition && classNames.push("dx-button-icon-right");
    return classNames.join(" ")
};
var viewFunction = exports.viewFunction = function(viewModel) {
    var _viewModel$props = viewModel.props,
        icon = _viewModel$props.icon,
        iconPosition = _viewModel$props.iconPosition,
        template = _viewModel$props.template,
        text = _viewModel$props.text;
    var renderText = !template && text;
    var isIconLeft = "left" === iconPosition;
    var iconComponent = !template && viewModel.iconSource && Preact.h(_icon3.default, {
        source: viewModel.iconSource,
        position: iconPosition
    });
    return Preact.h(_widget2.default, _extends({
        ref: viewModel.widgetRef,
        accessKey: viewModel.props.accessKey,
        activeStateEnabled: viewModel.props.activeStateEnabled,
        aria: viewModel.aria,
        classes: viewModel.cssClasses,
        disabled: viewModel.props.disabled,
        focusStateEnabled: viewModel.props.focusStateEnabled,
        height: viewModel.props.height,
        hint: viewModel.props.hint,
        hoverStateEnabled: viewModel.props.hoverStateEnabled,
        onActive: viewModel.onActive,
        onContentReady: viewModel.props.onContentReady,
        onClick: viewModel.onWidgetClick,
        onInactive: viewModel.onInactive,
        onKeyDown: viewModel.onWidgetKeyDown,
        rtlEnabled: viewModel.props.rtlEnabled,
        tabIndex: viewModel.props.tabIndex,
        visible: viewModel.props.visible,
        width: viewModel.props.width
    }, viewModel.restAttributes), Preact.h("div", {
        className: "dx-button-content",
        ref: viewModel.contentRef
    }, template && viewModel.props.template({
        data: {
            icon: icon,
            text: text
        },
        parentRef: viewModel.contentRef
    }), isIconLeft && iconComponent, renderText && Preact.h("span", {
        className: "dx-button-text"
    }, text), !isIconLeft && iconComponent, viewModel.props.useSubmitBehavior && Preact.h("input", {
        ref: viewModel.submitInputRef,
        type: "submit",
        tabIndex: -1,
        className: "dx-button-submit-input"
    }), viewModel.props.useInkRipple && Preact.h(_inkRipple2.default, {
        config: viewModel.inkRippleConfig,
        ref: viewModel.inkRippleRef
    })))
};
var ButtonProps = exports.ButtonProps = _objectSpread(_objectSpread({}, _baseProps2.default), {}, {
    activeStateEnabled: true,
    hoverStateEnabled: true,
    icon: "",
    iconPosition: "left",
    onClick: _noop2.default,
    onSubmit: _noop2.default,
    template: "",
    text: "",
    useInkRipple: false,
    useSubmitBehavior: false,
    validationGroup: void 0
});
var defaultOptionRules = exports.defaultOptionRules = (0, _utils.createDefaultOptionRules)([{
    device: function() {
        return "desktop" === _devices2.default.real().deviceType && !_devices2.default.isSimulator()
    },
    options: {
        focusStateEnabled: true
    }
}, {
    device: function() {
        return themes.isMaterial(themes.current())
    },
    options: {
        useInkRipple: true
    }
}]);

function getTemplate(props, template, render, component) {
    var getRender = function(render) {
        return function(props) {
            return "data" in props ? render(props.data, props.index) : render(props)
        }
    };
    var Component = props[component];
    return props[template] || props[render] && getRender(props[render]) || Component && function(props) {
        return Preact.h(Component, _extends({}, props))
    }
}
var Button = (0, _compat.forwardRef)(function(props, ref) {
    var contentRef = (0, _hooks.useRef)();
    var submitInputRef = (0, _hooks.useRef)();
    var inkRippleRef = (0, _hooks.useRef)();
    var widgetRef = (0, _hooks.useRef)();
    (0, _hooks.useImperativeHandle)(ref, function() {
        return {
            focus: function() {
                widgetRef.current.focus()
            }
        }
    }, []);
    var onActive = (0, _hooks.useCallback)(function(event) {
        var useInkRipple = props.useInkRipple;
        useInkRipple && inkRippleRef.current.showWave({
            element: contentRef.current,
            event: event
        })
    }, [props.useInkRipple]);
    var onInactive = (0, _hooks.useCallback)(function(event) {
        var useInkRipple = props.useInkRipple;
        useInkRipple && inkRippleRef.current.hideWave({
            element: contentRef.current,
            event: event
        })
    }, [props.useInkRipple]);
    var onWidgetClick = (0, _hooks.useCallback)(function(event) {
        var onClick = props.onClick,
            useSubmitBehavior = props.useSubmitBehavior,
            validationGroup = props.validationGroup;
        null === onClick || void 0 === onClick ? void 0 : onClick({
            event: event,
            validationGroup: validationGroup
        });
        useSubmitBehavior && submitInputRef.current.click()
    }, [props.onClick, props.useSubmitBehavior, props.validationGroup]);
    var onWidgetKeyDown = (0, _hooks.useCallback)(function(options) {
        var onKeyDown = props.onKeyDown;
        var keyName = options.keyName,
            originalEvent = options.originalEvent,
            which = options.which;
        var result = null === onKeyDown || void 0 === onKeyDown ? void 0 : onKeyDown(options);
        if (null === result || void 0 === result ? void 0 : result.cancel) {
            return result
        }
        if ("space" === keyName || "space" === which || "enter" === keyName || "enter" === which) {
            originalEvent.preventDefault();
            onWidgetClick(originalEvent)
        }
        return
    }, [props.onKeyDown, props.onClick, props.useSubmitBehavior, props.validationGroup]);
    var __aria = (0, _hooks.useCallback)(function() {
        var icon = props.icon,
            text = props.text;
        var label = text || icon;
        if (!text && icon && "image" === (0, _icon.getImageSourceType)(icon)) {
            label = icon.indexOf("base64") === -1 ? icon.replace(/.+\/([^.]+)\..+$/, "$1") : "Base64"
        }
        return _objectSpread({
            role: "button"
        }, label ? {
            label: label
        } : {})
    }, [props.icon, props.text]);
    var __cssClasses = (0, _hooks.useCallback)(function() {
        return getCssClasses(props)
    }, [props]);
    var __iconSource = (0, _hooks.useCallback)(function() {
        var icon = props.icon,
            type = props.type;
        return icon || "back" === type ? icon || "back" : ""
    }, [props.icon, props.type]);
    var __inkRippleConfig = (0, _hooks.useCallback)(function() {
        var icon = props.icon,
            text = props.text,
            type = props.type;
        return !text && icon || "back" === type ? {
            isCentered: true,
            useHoldAnimation: false,
            waveSizeCoefficient: 1
        } : {}
    }, [props.icon, props.text, props.type]);
    var __restAttributes = (0, _hooks.useCallback)(function() {
        var restProps = (props.accessKey, props.activeStateEnabled, props.component, props.disabled, props.focusStateEnabled, props.height, props.hint, props.hoverStateEnabled, props.icon, props.iconPosition, props.onClick, props.onContentReady, props.onKeyDown, props.onSubmit, props.pressed, props.render, props.rtlEnabled, props.stylingMode, props.tabIndex, props.template, props.text, props.type, props.useInkRipple, props.useSubmitBehavior, props.validationGroup, props.visible, props.width, _objectWithoutProperties(props, ["accessKey", "activeStateEnabled", "component", "disabled", "focusStateEnabled", "height", "hint", "hoverStateEnabled", "icon", "iconPosition", "onClick", "onContentReady", "onKeyDown", "onSubmit", "pressed", "render", "rtlEnabled", "stylingMode", "tabIndex", "template", "text", "type", "useInkRipple", "useSubmitBehavior", "validationGroup", "visible", "width"]));
        return restProps
    }, [props]);
    (0, _hooks.useEffect)(function() {
        var onContentReady = props.onContentReady;
        onContentReady({
            element: contentRef.current.parentNode
        })
    }, [props.onContentReady]);
    (0, _hooks.useEffect)(function() {
        var namespace = "UIFeedback";
        var onSubmit = props.onSubmit,
            useSubmitBehavior = props.useSubmitBehavior;
        if (useSubmitBehavior && onSubmit) {
            _short.click.on(submitInputRef.current, function(event) {
                return onSubmit({
                    event: event,
                    submitInput: submitInputRef.current
                })
            }, {
                namespace: namespace
            });
            return function() {
                return _short.click.off(submitInputRef.current, {
                    namespace: namespace
                })
            }
        }
        return
    }, [props.onSubmit, props.useSubmitBehavior]);
    return viewFunction({
        props: _objectSpread(_objectSpread({}, props), {}, {
            template: getTemplate(props, "template", "render", "component")
        }),
        contentRef: contentRef,
        submitInputRef: submitInputRef,
        inkRippleRef: inkRippleRef,
        widgetRef: widgetRef,
        onActive: onActive,
        onInactive: onInactive,
        onWidgetClick: onWidgetClick,
        onWidgetKeyDown: onWidgetKeyDown,
        aria: __aria(),
        cssClasses: __cssClasses(),
        iconSource: __iconSource(),
        inkRippleConfig: __inkRippleConfig(),
        restAttributes: __restAttributes()
    })
});
exports.default = Button;

function __createDefaultProps() {
    return _objectSpread(_objectSpread({}, ButtonProps), (0, _utils.convertRulesToOptions)(defaultOptionRules))
}
Button.defaultProps = __createDefaultProps();
var __defaultOptionRules = [];

function defaultOptions(rule) {
    __defaultOptionRules.push(rule);
    Button.defaultProps = _objectSpread(_objectSpread({}, __createDefaultProps()), (0, _utils.convertRulesToOptions)(__defaultOptionRules))
}
