var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 from 'react';
import { render, } from 'react-dom';
import { ConfigProvider, } from '@alifd/next';
import Drawer from '../drawer';
// 快捷调用
export default function drawerShow(props) {
    var onOk = props.onOk, onCancel = props.onCancel, onClose = props.onClose, content = props.content, others = __rest(props, ["onOk", "onCancel", "onClose", "content"]);
    var actionRef;
    // 合并 customOnOK。 如果返回值为 true 自动关闭 Drawer
    // eslint-disable-next-line @typescript-eslint/naming-convention
    var customOnOK = function (event) {
        var _a, _b, _c;
        if (onOk) {
            var result = onOk(event);
            if (result instanceof Promise) {
                (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, true);
                result.then(function (state) {
                    var _a, _b, _c;
                    if (state !== false) {
                        (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, false);
                        (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef);
                        return;
                    }
                    (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setOKLoading) === null || _c === void 0 ? void 0 : _c.call(actionRef, false);
                }).catch(function () {
                    // ignore
                });
            }
            if (typeof result === 'boolean' && result) {
                (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef);
            }
        }
        else {
            (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _c === void 0 ? void 0 : _c.call(actionRef);
        }
    };
    var customOnCancel = function (event) {
        var _a, _b, _c;
        if (onCancel) {
            var result = onCancel(event);
            if (result instanceof Promise) {
                (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setCancelLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, true);
                result.then(function (state) {
                    var _a, _b;
                    if (state !== false) {
                        (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.setCancelLoading) === null || _a === void 0 ? void 0 : _a.call(actionRef, false);
                        (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef);
                    }
                }).catch(function () {
                    // ignore
                });
            }
            if (typeof result === 'boolean' && result) {
                (_b = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _b === void 0 ? void 0 : _b.call(actionRef);
            }
        }
        else {
            (_c = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _c === void 0 ? void 0 : _c.call(actionRef);
        }
    };
    var ConfigModal = ConfigProvider.config(Drawer, {
        componentName: 'Drawer',
    });
    var container = document.createElement('div');
    container.setAttribute('id', 'next-quick-drawer');
    document.body.appendChild(container);
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    var newContext = ConfigProvider.getContext();
    render(React.createElement(ConfigProvider, __assign({}, newContext),
        React.createElement(ConfigModal, __assign({}, __assign(__assign({}, others), { visible: true, 
            // eslint-disable-next-line @typescript-eslint/no-shadow,@typescript-eslint/naming-convention
            actionRef: function (show, close, setOKLoading, setCancelLoading) {
                actionRef = {
                    show: show,
                    close: close,
                    setOKLoading: setOKLoading,
                    setCancelLoading: setCancelLoading,
                };
            }, onOk: customOnOK, onCancel: customOnCancel, onClose: onClose !== null && onClose !== void 0 ? onClose : (function () { var _a; return (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _a === void 0 ? void 0 : _a.call(actionRef); }) })), content)), container);
    return {
        hide: function () { var _a; return (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.close) === null || _a === void 0 ? void 0 : _a.call(actionRef); },
        show: function () { var _a; return (_a = actionRef === null || actionRef === void 0 ? void 0 : actionRef.show) === null || _a === void 0 ? void 0 : _a.call(actionRef); },
    };
}
