/**
 * 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 amisCore = require('amis-core');
var ConfirmBox = require('./ConfirmBox.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 PickerContainer = /** @class */ (function (_super) {
    tslib.__extends(PickerContainer, _super);
    function PickerContainer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            isOpened: false,
            value: _this.props.value
        };
        return _this;
    }
    PickerContainer.prototype.componentDidUpdate = function (prevProps) {
        var props = this.props;
        if (props.value !== prevProps.value) {
            this.setState({
                value: props.value
            });
        }
    };
    PickerContainer.prototype.handleClick = function () {
        var _a, _b;
        return tslib.__awaiter(this, void 0, void 0, function () {
            var state, _c;
            var _this = this;
            return tslib.__generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        _c = [{ value: this.props.value }];
                        return [4 /*yield*/, ((_b = (_a = this.props).onPickerOpen) === null || _b === void 0 ? void 0 : _b.call(_a, this.props))];
                    case 1:
                        state = tslib.__assign.apply(void 0, [tslib.__assign.apply(void 0, _c.concat([(_d.sent())])), { isOpened: true }]);
                        this.setState(state, function () { var _a, _b; return (_b = (_a = _this.props).onFocus) === null || _b === void 0 ? void 0 : _b.call(_a); });
                        return [2 /*return*/];
                }
            });
        });
    };
    PickerContainer.prototype.close = function (e, callback) {
        var _this = this;
        this.setState({
            isOpened: false
        }, function () {
            var _a, _b, _c, _d;
            (_b = (_a = _this.props).onClose) === null || _b === void 0 ? void 0 : _b.call(_a);
            if (callback) {
                callback();
                return;
            }
            (_d = (_c = _this.props).onCancel) === null || _d === void 0 ? void 0 : _d.call(_c);
        });
    };
    PickerContainer.prototype.handleChange = function (value) {
        this.setState({
            value: value
        });
    };
    PickerContainer.prototype.beforeConfirm = function (form) {
        var _a, _b;
        return tslib.__awaiter(this, void 0, void 0, function () {
            var _c, onConfirm, beforeConfirm, ret, _d, state;
            return tslib.__generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        _c = this.props, onConfirm = _c.onConfirm, beforeConfirm = _c.beforeConfirm;
                        if (!beforeConfirm) return [3 /*break*/, 2];
                        return [4 /*yield*/, (beforeConfirm === null || beforeConfirm === void 0 ? void 0 : beforeConfirm(form))];
                    case 1:
                        _d = _e.sent();
                        return [3 /*break*/, 4];
                    case 2: return [4 /*yield*/, ((_a = form === null || form === void 0 ? void 0 : form.submit) === null || _a === void 0 ? void 0 : _a.call(form))];
                    case 3:
                        _d = _e.sent();
                        _e.label = 4;
                    case 4:
                        ret = _d;
                        state = {
                            isOpened: false
                        };
                        // beforeConfirm 返回 false 则阻止后续动作
                        if (ret === false) {
                            return [2 /*return*/, false];
                        }
                        else if (amisCore.isObject(ret)) {
                            state.value = ret;
                        }
                        return [4 /*yield*/, (onConfirm === null || onConfirm === void 0 ? void 0 : onConfirm((_b = state.value) !== null && _b !== void 0 ? _b : this.state.value))];
                    case 5:
                        _e.sent();
                        this.setState(state);
                        return [2 /*return*/];
                }
            });
        });
    };
    PickerContainer.prototype.updateState = function (state) {
        if (state === void 0) { state = {}; }
        state.isOpened; var rest = tslib.__rest(state, ["isOpened"]);
        this.setState(tslib.__assign(tslib.__assign({}, this.state), rest));
    };
    PickerContainer.prototype.render = function () {
        var _this = this;
        var _a = this.props, children = _a.children, popOverRender = _a.bodyRender, title = _a.title, showTitle = _a.showTitle, headerClassName = _a.headerClassName, bodyClassName = _a.bodyClassName, className = _a.className, __ = _a.translate, size = _a.size, showFooter = _a.showFooter, closeOnEsc = _a.closeOnEsc, popOverContainer = _a.popOverContainer, mobileUI = _a.mobileUI, disabled = _a.disabled, testIdBuilder = _a.testIdBuilder;
        return (_J$X_(React__default["default"].Fragment, null,
            children({
                isOpened: this.state.isOpened,
                onClick: this.handleClick,
                setState: this.updateState
            }),
            _J$X_(ConfirmBox["default"], { type: "dialog", size: size, closeOnEsc: closeOnEsc, show: this.state.isOpened, onCancel: this.close, title: title || __('Select.placeholder'), showTitle: showTitle, headerClassName: headerClassName, bodyClassName: bodyClassName, className: className, showFooter: showFooter, beforeConfirm: this.beforeConfirm, popOverContainer: popOverContainer, mobileUI: mobileUI, disabled: disabled, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('confirm-box') }, function (_a) {
                var popOverContainer = _a.popOverContainer, loading = _a.loading, onConfirm = _a.onConfirm, bodyRef = _a.bodyRef;
                return popOverRender === null || popOverRender === void 0 ? void 0 : popOverRender(tslib.__assign(tslib.__assign({}, _this.state), { ref: bodyRef, setState: _this.updateState, onClose: _this.close, onChange: _this.handleChange, onConfirm: onConfirm, popOverContainer: popOverContainer, loading: loading }));
            })));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", Promise)
    ], PickerContainer.prototype, "handleClick", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, Function]),
        tslib.__metadata("design:returntype", void 0)
    ], PickerContainer.prototype, "close", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], PickerContainer.prototype, "handleChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", Promise)
    ], PickerContainer.prototype, "beforeConfirm", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], PickerContainer.prototype, "updateState", null);
    return PickerContainer;
}(React__default["default"].Component));
var PickerContainer$1 = amisCore.themeable(amisCore.localeable(PickerContainer));

exports.PickerContainer = PickerContainer;
exports["default"] = PickerContainer$1;
