/**
 * DevExtreme (ui/speed_dial_action/speed_dial_main_item.js)
 * Version: 19.1.8 (build 19359-1325)
 * Build date: Wed Dec 25 2019
 *
 * Copyright (c) 2012 - 2019 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
var _createClass = function() {
    function defineProperties(target, props) {
        for (var i = 0; i < props.length; i++) {
            var descriptor = props[i];
            descriptor.enumerable = descriptor.enumerable || false;
            descriptor.configurable = true;
            if ("value" in descriptor) {
                descriptor.writable = true
            }
            Object.defineProperty(target, descriptor.key, descriptor)
        }
    }
    return function(Constructor, protoProps, staticProps) {
        if (protoProps) {
            defineProperties(Constructor.prototype, protoProps)
        }
        if (staticProps) {
            defineProperties(Constructor, staticProps)
        }
        return Constructor
    }
}();
var _get = function get(object, property, receiver) {
    if (null === object) {
        object = Function.prototype
    }
    var desc = Object.getOwnPropertyDescriptor(object, property);
    if (void 0 === desc) {
        var parent = Object.getPrototypeOf(object);
        if (null === parent) {
            return
        } else {
            return get(parent, property, receiver)
        }
    } else {
        if ("value" in desc) {
            return desc.value
        } else {
            var getter = desc.get;
            if (void 0 === getter) {
                return
            }
            return getter.call(receiver)
        }
    }
};
var _renderer = require("../../core/renderer");
var _renderer2 = _interopRequireDefault(_renderer);
var _config = require("../../core/config");
var _config2 = _interopRequireDefault(_config);
var _extend = require("../../core/utils/extend");
var _common = require("../../core/utils/common");
var _events_engine = require("../../events/core/events_engine");
var _events_engine2 = _interopRequireDefault(_events_engine);
var _ui = require("../widget/ui.errors");
var _ui2 = _interopRequireDefault(_ui);
var _swatch_container = require("../widget/swatch_container");
var _speed_dial_item = require("./speed_dial_item");
var _speed_dial_item2 = _interopRequireDefault(_speed_dial_item);
var _themes = require("../themes");
var _themes2 = _interopRequireDefault(_themes);

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

function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function")
    }
}

function _possibleConstructorReturn(self, call) {
    if (!self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return call && ("object" === typeof call || "function" === typeof call) ? call : self
}

function _inherits(subClass, superClass) {
    if ("function" !== typeof superClass && null !== superClass) {
        throw new TypeError("Super expression must either be null or a function, not " + typeof superClass)
    }
    subClass.prototype = Object.create(superClass && superClass.prototype, {
        constructor: {
            value: subClass,
            enumerable: false,
            writable: true,
            configurable: true
        }
    });
    if (superClass) {
        Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass
    }
}
var FAB_MAIN_CLASS = "dx-fa-button-main";
var FAB_CLOSE_ICON_CLASS = "dx-fa-button-icon-close";
var INVISIBLE_STATE_CLASS = "dx-state-invisible";
var speedDialMainItem = null;
var SpeedDialMainItem = function(_SpeedDialItem) {
    _inherits(SpeedDialMainItem, _SpeedDialItem);

    function SpeedDialMainItem() {
        _classCallCheck(this, SpeedDialMainItem);
        return _possibleConstructorReturn(this, (SpeedDialMainItem.__proto__ || Object.getPrototypeOf(SpeedDialMainItem)).apply(this, arguments))
    }
    _createClass(SpeedDialMainItem, [{
        key: "_getDefaultOptions",
        value: function() {
            var defaultOptions = {
                icon: "add",
                closeIcon: "close",
                position: {
                    at: "right bottom",
                    my: "right bottom",
                    offset: {
                        x: -16,
                        y: -16
                    }
                },
                maxSpeedDialActionCount: 5,
                hint: "",
                actions: [],
                visible: true,
                activeStateEnabled: true,
                hoverStateEnabled: true,
                indent: 56,
                childIndent: 40,
                callOverlayRenderShading: true
            };
            return (0, _extend.extend)(_get(SpeedDialMainItem.prototype.__proto__ || Object.getPrototypeOf(SpeedDialMainItem.prototype), "_getDefaultOptions", this).call(this), (0, _extend.extend)(defaultOptions, (0, _config2.default)().floatingActionButtonConfig))
        }
    }, {
        key: "_defaultOptionsRules",
        value: function() {
            return _get(SpeedDialMainItem.prototype.__proto__ || Object.getPrototypeOf(SpeedDialMainItem.prototype), "_defaultOptionsRules", this).call(this).concat([{
                device: function() {
                    return _themes2.default.isMaterial()
                },
                options: {
                    indent: 72,
                    childIndent: 56
                }
            }])
        }
    }, {
        key: "_render",
        value: function() {
            this.$element().addClass(FAB_MAIN_CLASS);
            _get(SpeedDialMainItem.prototype.__proto__ || Object.getPrototypeOf(SpeedDialMainItem.prototype), "_render", this).call(this);
            this._moveToContainer();
            this._renderCloseIcon();
            this._renderClick()
        }
    }, {
        key: "_renderCloseIcon",
        value: function() {
            this._$closeIcon = this._renderButtonIcon(this._$closeIcon, this._options.closeIcon, FAB_CLOSE_ICON_CLASS);
            this._$closeIcon.addClass(INVISIBLE_STATE_CLASS)
        }
    }, {
        key: "_renderClick",
        value: function() {
            this._clickAction = 1 === this.option("actions").length ? this._getActionComponent()._createActionByOption("onClick") : this._createAction(this._clickHandler.bind(this));
            this._setClickAction()
        }
    }, {
        key: "_clickHandler",
        value: function() {
            var _this2 = this;
            var actions = this._actionItems;
            actions.forEach(function(action) {
                action.toggle();
                if (action.option("visible")) {
                    action._$wrapper.css("position", _this2._$wrapper.css("position"))
                }
            });
            this._$icon.toggleClass(INVISIBLE_STATE_CLASS);
            this._$closeIcon.toggleClass(INVISIBLE_STATE_CLASS)
        }
    }, {
        key: "_renderActions",
        value: function() {
            var _this3 = this;
            var actions = this.option("actions");
            var lastActionIndex = actions.length - 1;
            var minActionButtonCount = 1;
            if (this._actionItems && this._actionItems.length) {
                this._actionItems.forEach(function(actionItem) {
                    actionItem.dispose();
                    actionItem.$element().remove()
                })
            }
            this._actionItems = [];
            if (actions.length === minActionButtonCount) {
                return
            }
            for (var i = 0; i < actions.length; i++) {
                var action = actions[i];
                var $actionElement = (0, _renderer2.default)("<div>").appendTo((0, _swatch_container.getSwatchContainer)(action.$element()));
                _events_engine2.default.off($actionElement, "click");
                _events_engine2.default.on($actionElement, "click", function() {
                    _this3._clickHandler()
                });
                var actionOffsetY = this.initialOption("indent") + this.initialOption("childIndent") * i;
                var actionAnimationDelay = 30;
                action._options.position = {
                    of: this.$content(),
                    at: "center",
                    my: "center",
                    offset: {
                        x: 0,
                        y: -actionOffsetY
                    }
                };
                action._options.animation.show.delay = actionAnimationDelay * i;
                action._options.animation.hide.delay = actionAnimationDelay * (lastActionIndex - i);
                action._options.actionComponent = action;
                this._actionItems.push(this._createComponent($actionElement, _speed_dial_item2.default, action._options))
            }
        }
    }, {
        key: "_setPosition",
        value: function() {
            this._hide();
            this._show()
        }
    }, {
        key: "_optionChanged",
        value: function(args) {
            switch (args.name) {
                case "actions":
                    this._renderIcon();
                    this._renderCloseIcon();
                    this._renderClick();
                    this._renderActions();
                    break;
                case "maxSpeedDialActionCount":
                    this._renderActions();
                    break;
                case "closeIcon":
                    this._renderCloseIcon();
                    break;
                case "position":
                    if (!(0, _common.equalByValue)(args.value, args.previousValue)) {
                        this._setPosition()
                    }
                    break;
                default:
                    _get(SpeedDialMainItem.prototype.__proto__ || Object.getPrototypeOf(SpeedDialMainItem.prototype), "_optionChanged", this).call(this, args)
            }
        }
    }]);
    return SpeedDialMainItem
}(_speed_dial_item2.default);
exports.initAction = function(newAction) {
    delete newAction._options.onInitializing;
    var isActionExist = false;
    if (!speedDialMainItem) {
        var $fabMainElement = (0, _renderer2.default)("<div>").appendTo((0, _swatch_container.getSwatchContainer)(newAction.$element()));
        speedDialMainItem = newAction._createComponent($fabMainElement, SpeedDialMainItem, (0, _extend.extend)({}, newAction._options, {
            actions: [newAction],
            visible: true
        }))
    } else {
        var savedActions = speedDialMainItem.option("actions");
        savedActions.forEach(function(action) {
            if (action._options.id === newAction._options.id) {
                isActionExist = true;
                return newAction
            }
        });
        if (!isActionExist) {
            if (savedActions.length >= speedDialMainItem.option("maxSpeedDialActionCount")) {
                newAction.dispose();
                _ui2.default.log("W1014");
                return
            }
            savedActions.push(newAction);
            speedDialMainItem.option((0, _extend.extend)(speedDialMainItem._getDefaultOptions(), {
                actions: savedActions
            }))
        } else {
            if (1 === savedActions.length) {
                speedDialMainItem.option((0, _extend.extend)({}, newAction._options, {
                    actions: savedActions,
                    visible: true,
                    position: speedDialMainItem._getDefaultOptions().position
                }))
            } else {
                speedDialMainItem.option({
                    actions: savedActions,
                    position: speedDialMainItem._getDefaultOptions().position
                })
            }
        }
    }
};
exports.disposeAction = function(actionId) {
    if (!speedDialMainItem) {
        return
    }
    var savedActions = speedDialMainItem.option("actions");
    var savedActionsCount = savedActions.length;
    savedActions = savedActions.filter(function(action) {
        return action._options.id !== actionId
    });
    if (savedActionsCount === savedActions.length) {
        return
    }
    if (!savedActions.length) {
        speedDialMainItem.dispose();
        speedDialMainItem.$element().remove();
        speedDialMainItem = null
    } else {
        if (1 === savedActions.length) {
            speedDialMainItem.option((0, _extend.extend)({}, savedActions[0]._options, {
                actions: savedActions,
                visible: true,
                position: speedDialMainItem._getDefaultOptions().position
            }))
        } else {
            speedDialMainItem.option({
                actions: savedActions
            })
        }
    }
};
exports.repaint = function() {
    if (!speedDialMainItem) {
        return
    }
    var icon = 1 === speedDialMainItem.option("actions").length ? speedDialMainItem.option("actions")[0].option("icon") : speedDialMainItem._getDefaultOptions().icon;
    speedDialMainItem.option({
        actions: speedDialMainItem.option("actions"),
        icon: icon,
        closeIcon: speedDialMainItem._getDefaultOptions().closeIcon,
        position: speedDialMainItem._getDefaultOptions().position,
        maxSpeedDialActionCount: speedDialMainItem._getDefaultOptions().maxSpeedDialActionCount
    })
};
