import _extends from "babel-runtime/helpers/extends";
import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";
import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn";
import _inherits from "babel-runtime/helpers/inherits";
var __rest = this && this.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};
import React, { Component } from "react";
import AntdModal from "antd/es/modal";
import PagePoint from "../page-point/pagePointEnhanced";
import { ConfigContext } from "antd/es/config-provider/context";
import { ConfigConsumer } from "../config-provider";
import "antd/es/modal/style";
var confirm = AntdModal.confirm,
    info = AntdModal.info,
    success = AntdModal.success,
    error = AntdModal.error,
    warning = AntdModal.warning;

var destroyAll = AntdModal.destroyAll;

var Modal = function (_Component) {
    _inherits(Modal, _Component);

    function Modal(props) {
        _classCallCheck(this, Modal);

        var _this = _possibleConstructorReturn(this, (Modal.__proto__ || Object.getPrototypeOf(Modal)).call(this, props));

        _this.mouseDownX = 0;
        _this.mouseDownY = 0;
        _this.deltaX = 0;
        _this.deltaY = 0;
        _this.sumX = 0;
        _this.sumY = 0;
        _this.state = {
            visible: false
        };
        _this.handleMove = function (event) {
            var deltaX = event.pageX - _this.mouseDownX;
            var deltaY = event.pageY - _this.mouseDownY;
            _this.deltaX = deltaX;
            _this.deltaY = deltaY;
            _this.modalContent.style.transform = "translate(" + (deltaX + _this.sumX) + "px, " + (deltaY + _this.sumY) + "px)";
        };
        _this.show = function () {
            _this.setState({ visible: true });
        };
        _this.hide = function () {
            _this.setState({ visible: false });
        };
        _this.initialEvent = function (visible) {
            var title = _this.props.title;

            var prefix = _this.context ? _this.context.getPrefixCls('modal') : 'ant-modal';
            if (title && visible) {
                setTimeout(function () {
                    window.removeEventListener("mouseup", _this.removeUp, false);
                    _this.contain = document.getElementsByClassName(_this.simpleClass)[0];
                    _this.header = _this.contain.getElementsByClassName(prefix + "-header")[0];
                    _this.modalContent = _this.contain.getElementsByClassName(prefix + "-content")[0];
                    if (_this.header) {
                        _this.header.style.cursor = "all-scroll";
                        _this.header.onmousedown = function (e) {
                            _this.mouseDownX = e.pageX;
                            _this.mouseDownY = e.pageY;
                            document.body.onselectstart = function () {
                                return false;
                            };
                            window.addEventListener("mousemove", _this.handleMove, false);
                        };
                    }
                    window.addEventListener("mouseup", _this.removeUp, false);
                }, 0);
            }
            if (!visible) {
                _this.mouseDownX = 0;
                _this.mouseDownY = 0;
                _this.deltaX = 0;
                _this.deltaY = 0;
                _this.sumX = 0;
                _this.sumY = 0;
            }
        };
        _this.removeMove = function () {
            window.removeEventListener("mousemove", _this.handleMove, false);
        };
        _this.removeUp = function () {
            document.body.onselectstart = function () {
                return true;
            };
            _this.sumX = _this.sumX + _this.deltaX;
            _this.sumY = _this.sumY + _this.deltaY;
            _this.removeMove();
        };
        _this.simpleClass = Math.random().toString(36).substring(2);
        return _this;
    }

    _createClass(Modal, [{
        key: "componentDidMount",
        value: function componentDidMount() {
            var _props$draggable = this.props.draggable,
                draggable = _props$draggable === undefined ? true : _props$draggable;

            if (draggable) {
                this.initialEvent(this.props.visible);
            }
        }
    }, {
        key: "componentWillUnmount",
        value: function componentWillUnmount() {
            var _props$draggable2 = this.props.draggable,
                draggable = _props$draggable2 === undefined ? true : _props$draggable2;

            if (draggable) {
                this.removeMove();
                window.removeEventListener("mouseup", this.removeUp, false);
            }
        }
    }, {
        key: "componentDidUpdate",
        value: function componentDidUpdate() {
            var _props$draggable3 = this.props.draggable,
                draggable = _props$draggable3 === undefined ? true : _props$draggable3;

            if (draggable) {
                this.initialEvent(this.props.visible);
            }
        }
    }, {
        key: "render",
        value: function render() {
            var _this2 = this;

            var _a = this.props,
                children = _a.children,
                wrapClassName = _a.wrapClassName,
                other = __rest(_a, ["children", "wrapClassName"]);
            var defaultOpts = {
                destroyOnClose: true
            };
            var opts = _extends(_extends({}, defaultOpts), other);
            if (!Object.prototype.hasOwnProperty.call(opts, 'visible') || typeof opts.visible !== 'boolean') {
                opts.visible = this.state.visible;
            }
            var wrapModalClassName = wrapClassName ? wrapClassName + " " + this.simpleClass : "" + this.simpleClass;
            return React.createElement(
                ConfigConsumer,
                null,
                function (_ref) {
                    var pagePoint = _ref.pagePoint;
                    return pagePoint ? React.createElement(PagePoint, _extends({ compType: "Modal" }, _this2.props, { render: function render(eventprops) {
                            return React.createElement(
                                AntdModal,
                                _extends({}, opts, eventprops, { wrapClassName: wrapModalClassName, "data-ui-role": "Modal" }),
                                children
                            );
                        } })) : React.createElement(
                        AntdModal,
                        _extends({}, opts, { wrapClassName: wrapModalClassName }),
                        children
                    );
                }
            );
        }
    }], [{
        key: "getDerivedStateFromProps",
        value: function getDerivedStateFromProps(nextProps, prevState) {
            var visible = nextProps.visible;

            if (typeof visible === 'boolean' && visible !== prevState.visible) {
                return {
                    visible: visible
                };
            }
            return null;
        }
    }]);

    return Modal;
}(Component);

export default Modal;

Modal.destroyAll = destroyAll;
Modal.contextType = ConfigContext;
Modal.info = info;
Modal.confirm = confirm;
Modal.success = success;
Modal.error = error;
Modal.warning = warning;