/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var ReactDOM = require('react-dom');
var Modal = require('./Modal.js');
var Button = require('./Button.js');
var amisCore = require('amis-core');
var Html = require('./Html.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var Alert = /** @class */ (function (_super) {
    tslib.__extends(Alert, _super);
    function Alert(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            show: false,
            title: '',
            content: '',
            confirm: false,
            size: '',
            confirmBtnLevel: 'danger',
            cancelBtnLevel: 'default',
            closeOnEsc: false,
            confirmText: '确认',
            cancelText: '取消'
        };
        _this.unmount = false;
        _this.close = _this.close.bind(_this);
        _this.handleConfirm = _this.handleConfirm.bind(_this);
        _this.handleCancel = _this.handleCancel.bind(_this);
        _this.modalRef = _this.modalRef.bind(_this);
        _this.handleFormSubmit = _this.handleFormSubmit.bind(_this);
        _this.scopeRef = _this.scopeRef.bind(_this);
        if (!props.isolate) {
            _this.originInstance = Alert.instance;
            Alert.instance = _this;
        }
        return _this;
    }
    Alert.getInstance = function () {
        return tslib.__awaiter(this, void 0, void 0, function () {
            var container, div;
            return tslib.__generator(this, function (_a) {
                if (!Alert.instance || Alert.instance.unmount) {
                    console.warn('Alert 组件应该没有被渲染，所以隐性的渲染到 body 了');
                    container = document.body;
                    div = document.createElement('div');
                    container.appendChild(div);
                    // if (parseInt(version.split('.')[0], 10) >= 18) {
                    //   const root = createRoot(div);
                    //   await new Promise<void>(resolve =>
                    //     root.render(<FinnalAlert ref={() => resolve()} />)
                    //   );
                    // } else {
                    ReactDOM.render(_J$X_(FinnalAlert, null), div);
                    // }
                }
                return [2 /*return*/, Alert.instance];
            });
        });
    };
    Alert.prototype.componentDidMount = function () {
        this._body && (this._body.innerHTML = this.state.content);
    };
    Alert.prototype.componentDidUpdate = function (prevProps, prevState) {
        if (prevState.content !== this.state.content) {
            this._body && (this._body.innerHTML = this.state.content);
        }
    };
    Alert.prototype.componentWillUnmount = function () {
        this.unmount = true;
        if (Alert.instance === this) {
            Alert.instance = this.originInstance || null;
            this.originInstance = null;
        }
    };
    Alert.prototype.scopeRef = function (schemaSope) {
        this.schemaSope = schemaSope;
    };
    Alert.prototype.handleConfirm = function () {
        var _a;
        var form = (_a = this.schemaSope) === null || _a === void 0 ? void 0 : _a.getComponentByName('form');
        if (form) {
            form.doAction({ type: 'submit' });
        }
        else {
            this.close(true);
        }
    };
    Alert.prototype.handleCancel = function () {
        this.close(false);
    };
    Alert.prototype.close = function (confirmed) {
        var _this = this;
        var isConfirm = this.state.confirm || this.state.prompt;
        this.setState({
            show: false,
            prompt: false,
            confirm: false
        }, isConfirm ? function () { return _this._resolve(confirmed); } /*this._reject()*/ : undefined);
    };
    Alert.prototype.alert = function (content, title, className) {
        this.setState({
            title: title,
            content: content,
            show: true,
            confirm: false,
            className: className
        });
    };
    Alert.prototype.confirm = function (content, title, optionsOrCofnrimText, cancelText) {
        var _this = this;
        this.setState({
            title: title,
            content: content,
            show: true,
            confirm: true,
            confirmText: typeof optionsOrCofnrimText === 'string'
                ? optionsOrCofnrimText
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.confirmText,
            cancelText: cancelText,
            size: typeof optionsOrCofnrimText === 'string'
                ? ''
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.size,
            closeOnEsc: typeof optionsOrCofnrimText === 'string'
                ? false
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.closeOnEsc,
            className: typeof optionsOrCofnrimText === 'string'
                ? ''
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.className,
            confirmBtnLevel: typeof optionsOrCofnrimText === 'string'
                ? 'danger'
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.confirmBtnLevel,
            cancelBtnLevel: typeof optionsOrCofnrimText === 'string'
                ? 'default'
                : optionsOrCofnrimText === null || optionsOrCofnrimText === void 0 ? void 0 : optionsOrCofnrimText.cancelBtnLevel
        });
        return new Promise(function (resolve) {
            _this._resolve = resolve;
        });
    };
    Alert.prototype.prompt = function (controls, defaultValue, title, confirmText) {
        var _this = this;
        if (title === void 0) { title = 'placeholder.enter'; }
        if (confirmText === void 0) { confirmText = 'confirm'; }
        if (typeof controls === 'string') {
            // 兼容浏览器标准用法。
            controls = [
                {
                    name: 'text',
                    label: controls,
                    type: 'text'
                }
            ];
            if (typeof defaultValue === 'string') {
                defaultValue = {
                    text: defaultValue
                };
            }
        }
        else if (!Array.isArray(controls)) {
            controls = [controls];
        }
        this.setState({
            title: title,
            controls: controls,
            show: true,
            prompt: true,
            value: defaultValue,
            confirmText: confirmText
        });
        return new Promise(function (resolve) {
            _this._resolve = resolve;
        });
    };
    Alert.prototype.modalRef = function (ref) {
        this._modal = ref;
    };
    Alert.prototype.handleFormSubmit = function (values) {
        this.close(values);
    };
    Alert.prototype.render = function () {
        var _a, _b, _c, _d, _e;
        var _f = this.props, container = _f.container, cancelText = _f.cancelText, confirmText = _f.confirmText, title = _f.title, confirmBtnLevel = _f.confirmBtnLevel, alertBtnLevel = _f.alertBtnLevel, cancelBtnLevel = _f.cancelBtnLevel, cx = _f.classnames;
        var theme = this.props.theme || 'cxd';
        if (theme === 'default') {
            theme = 'cxd';
        }
        var __ = this.props.translate;
        var finalTitle = __((_a = this.state.title) !== null && _a !== void 0 ? _a : title);
        var finalConfirmText = __((_b = this.state.confirmText) !== null && _b !== void 0 ? _b : confirmText);
        var finalCancelText = __((_c = this.state.cancelText) !== null && _c !== void 0 ? _c : cancelText);
        var finalConfirmBtnLevel = (_d = this.state.confirmBtnLevel) !== null && _d !== void 0 ? _d : confirmBtnLevel;
        var finalCancelBtnLevel = (_e = this.state.cancelBtnLevel) !== null && _e !== void 0 ? _e : cancelBtnLevel;
        return (_J$X_(Modal["default"], { show: this.state.show, onHide: this.handleCancel, container: container, ref: this.modalRef, closeOnEsc: this.state.closeOnEsc, size: this.state.size, className: cx(this.state.className) },
            finalTitle ? (_J$X_("div", { className: cx('Modal-header') },
                _J$X_("div", { className: cx('Modal-title') }, finalTitle))) : null,
            _J$X_("div", { className: cx('Modal-body') }, this.state.prompt ? (renderForm(this.state.controls, this.state.value, this.handleFormSubmit, this.scopeRef, theme)) : typeof this.state.content === 'string' ? (_J$X_(Html["default"], { html: this.state.content })) : (_J$X_(React__default["default"].Fragment, null, this.state.content))),
            finalConfirmText ? (_J$X_("div", { className: cx('Modal-footer') },
                this.state.confirm || this.state.prompt ? (_J$X_(Button["default"], { level: finalCancelBtnLevel, onClick: this.handleCancel }, __(finalCancelText))) : null,
                _J$X_(Button["default"], { level: this.state.confirm || this.state.prompt
                        ? finalConfirmBtnLevel
                        : alertBtnLevel, onClick: this.handleConfirm }, finalConfirmText))) : null));
    };
    Alert.instance = null;
    Alert.defaultProps = {
        confirmText: 'confirm',
        cancelText: 'cancel',
        title: 'Alert.info',
        alertBtnLevel: 'primary',
        confirmBtnLevel: 'danger',
        cancelBtnLevel: 'default'
    };
    return Alert;
}(React__default["default"].Component));
var renderSchemaFn;
function setRenderSchemaFn(fn) {
    renderSchemaFn = fn;
}
function renderForm(controls, value, callback, scopeRef, theme) {
    if (value === void 0) { value = {}; }
    return renderSchemaFn === null || renderSchemaFn === void 0 ? void 0 : renderSchemaFn(controls, value, callback, scopeRef, theme);
}
var alert = function (content, title, className) { return tslib.__awaiter(void 0, void 0, void 0, function () {
    var instance;
    return tslib.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, Alert.getInstance()];
            case 1:
                instance = _a.sent();
                return [2 /*return*/, instance.alert(content, title, className)];
        }
    });
}); };
var confirm = function (content, title, optionsOrCofnrimText, cancelText) { return tslib.__awaiter(void 0, void 0, void 0, function () {
    var instance;
    return tslib.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, Alert.getInstance()];
            case 1:
                instance = _a.sent();
                return [2 /*return*/, instance.confirm(content, title, optionsOrCofnrimText, cancelText)];
        }
    });
}); };
var prompt = function (controls, defaultvalue, title, confirmText) { return tslib.__awaiter(void 0, void 0, void 0, function () {
    var instance;
    return tslib.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4 /*yield*/, Alert.getInstance()];
            case 1:
                instance = _a.sent();
                return [2 /*return*/, instance.prompt(controls, defaultvalue, title, confirmText)];
        }
    });
}); };
var FinnalAlert = amisCore.themeable(amisCore.localeable(Alert));

exports.Alert = Alert;
exports.FinnalAlert = FinnalAlert;
exports.alert = alert;
exports.confirm = confirm;
exports["default"] = FinnalAlert;
exports.prompt = prompt;
exports.setRenderSchemaFn = setRenderSchemaFn;
