import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import * as ReactDOM from 'react-dom';
import { Component, createRef } from 'react';
import uniqueId from '../utils/uniqueId';
import noop from '../utils/noop';
import createElement from '../utils/dom/createElement';
import isBrowser from '../utils/isBrowser';
import Dialog from './Dialog';
var dialogInstanceMap = new Map();
function ensureUniqDialogInstance(dialogId) {
    if (dialogInstanceMap.has(dialogId)) {
        throw new Error("Duplicate dialog id found: " + dialogId);
    }
}
function addDialogInstance(dialogId, ref) {
    dialogInstanceMap.set(dialogId, ref);
}
var StandaloneDialog = (function (_super) {
    __extends(StandaloneDialog, _super);
    function StandaloneDialog() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            visible: true,
        };
        _this.closeOptions = {};
        _this.onClosed = function () {
            var _a = _this.props, onClose = _a.options.onClose, container = _a.container;
            var _b = _this.closeOptions.triggerOnClose, triggerOnClose = _b === void 0 ? true : _b;
            if (triggerOnClose && onClose) {
                onClose();
            }
            ReactDOM.unmountComponentAtNode(container);
        };
        _this.onClose = function (e) {
            _this.close({
                triggerOnClose: e !== false,
            });
        };
        return _this;
    }
    StandaloneDialog.prototype.close = function (options) {
        if (options === void 0) { options = {}; }
        this.closeOptions = options;
        this.setState({
            visible: false,
        });
    };
    StandaloneDialog.prototype.componentWillUnmount = function () {
        var dialogId = this.props.options.dialogId;
        dialogInstanceMap.delete(dialogId);
    };
    StandaloneDialog.prototype.render = function () {
        var options = this.props.options;
        var visible = this.state.visible;
        return (_jsx(Dialog, __assign({}, options, { onClose: this.onClose, onClosed: this.onClosed, visible: visible }), void 0));
    };
    return StandaloneDialog;
}(Component));
export function closeDialog(dialogId, options) {
    if (options === void 0) { options = {}; }
    var dialog = dialogInstanceMap.get(dialogId);
    if (!dialog) {
        return;
    }
    var wrapper = dialog.current;
    if (!wrapper) {
        return;
    }
    wrapper.close(options);
}
export function openDialog(options) {
    if (options === void 0) { options = {}; }
    if (!isBrowser)
        return noop;
    var _a = options.dialogId, dialogId = _a === void 0 ? uniqueId('__zent-dialog__') : _a, parentComponent = options.parentComponent;
    ensureUniqDialogInstance(dialogId);
    var container = createElement('div');
    var closeHandler = function (triggerOnClose) {
        if (triggerOnClose === void 0) { triggerOnClose = true; }
        closeDialog(dialogId, {
            triggerOnClose: triggerOnClose !== false,
        });
    };
    var render = parentComponent
        ? ReactDOM.unstable_renderSubtreeIntoContainer.bind(ReactDOM, parentComponent)
        : ReactDOM.render;
    var ref = createRef();
    render(_jsx(StandaloneDialog, { ref: ref, options: __assign(__assign({}, options), { dialogId: dialogId }), container: container }, void 0), container);
    addDialogInstance(dialogId, ref);
    return closeHandler;
}
