/**
 * amis v3.1.0
 * Copyright 2018-2023 baidu
 */

import { __extends, __awaiter, __generator, __decorate, __metadata } from 'tslib';
import { getPropValue, resolveEventData, setVariable, createObject, ScopedContext, autobind, Renderer } from 'amis-core';
import React from 'react';
import { SearchBox } from 'amis-ui';

/** @class */ ((function (_super) {
    __extends(SearchBoxRenderer, _super);
    function SearchBoxRenderer(props, context) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            value: getPropValue(props) || ''
        };
        var scoped = context;
        scoped.registerComponent(_this);
        return _this;
    }
    SearchBoxRenderer.prototype.componentWillUnmount = function () {
        var scoped = this.context;
        scoped.unRegisterComponent(this);
    };
    SearchBoxRenderer.prototype.handleChange = function (value) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, onChange, dispatchEvent, rendererEvent;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, onChange = _a.onChange, dispatchEvent = _a.dispatchEvent;
                        this.setState({ value: value });
                        return [4 /*yield*/, dispatchEvent('change', resolveEventData(this.props, {
                                value: value
                            }))];
                    case 1:
                        rendererEvent = _b.sent();
                        if (rendererEvent === null || rendererEvent === void 0 ? void 0 : rendererEvent.prevented) {
                            return [2 /*return*/];
                        }
                        onChange === null || onChange === void 0 ? void 0 : onChange(value);
                        return [2 /*return*/];
                }
            });
        });
    };
    SearchBoxRenderer.prototype.handleCancel = function () {
        var name = this.props.name;
        var onQuery = this.props.onQuery;
        var value = getPropValue(this.props);
        if (value !== '') {
            var data = {};
            setVariable(data, name, '');
            onQuery === null || onQuery === void 0 ? void 0 : onQuery(data);
        }
    };
    SearchBoxRenderer.prototype.handleSearch = function (text) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, name, onQuery, dispatchEvent, data, rendererEvent;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, name = _a.name, onQuery = _a.onQuery, dispatchEvent = _a.dispatchEvent;
                        data = {};
                        setVariable(data, name, text);
                        return [4 /*yield*/, dispatchEvent('search', createObject(this.props.data, data))];
                    case 1:
                        rendererEvent = _b.sent();
                        if (rendererEvent === null || rendererEvent === void 0 ? void 0 : rendererEvent.prevented) {
                            return [2 /*return*/];
                        }
                        onQuery === null || onQuery === void 0 ? void 0 : onQuery(data);
                        return [2 /*return*/];
                }
            });
        });
    };
    SearchBoxRenderer.prototype.dispatchEvent = function (name) {
        var dispatchEvent = this.props.dispatchEvent;
        dispatchEvent(name, resolveEventData(this.props, { value: this.state.value }));
    };
    SearchBoxRenderer.prototype.doAction = function (action, args) {
        var actionType = action === null || action === void 0 ? void 0 : action.actionType;
        if (actionType === 'clear') {
            this.setState({ value: '' });
        }
    };
    SearchBoxRenderer.prototype.setData = function (value) {
        this.setState({ value: value });
    };
    SearchBoxRenderer.prototype.render = function () {
        var _this = this;
        var _a = this.props; _a.data; var name = _a.name, onQuery = _a.onQuery, mini = _a.mini, enhance = _a.enhance, clearable = _a.clearable, searchImediately = _a.searchImediately, clearAndSubmit = _a.clearAndSubmit, placeholder = _a.placeholder, onChange = _a.onChange, className = _a.className, style = _a.style;
        var value = this.state.value;
        return (React.createElement(SearchBox, { className: className, style: style, name: name, disabled: !onQuery, defaultActive: !!value, defaultValue: onChange ? undefined : value, value: value, mini: mini, enhance: enhance, clearable: clearable, searchImediately: searchImediately, clearAndSubmit: clearAndSubmit, onSearch: this.handleSearch, onCancel: this.handleCancel, placeholder: placeholder, onChange: this.handleChange, onFocus: function () { return _this.dispatchEvent('focus'); }, onBlur: function () { return _this.dispatchEvent('blur'); } }));
    };
    SearchBoxRenderer.defaultProps = {
        name: 'keywords',
        mini: false,
        enhance: false,
        clearable: false,
        searchImediately: false,
        clearAndSubmit: false
    };
    SearchBoxRenderer.contextType = ScopedContext;
    SearchBoxRenderer.propsList = ['mini', 'searchImediately'];
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", Promise)
    ], SearchBoxRenderer.prototype, "handleChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], SearchBoxRenderer.prototype, "handleCancel", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", Promise)
    ], SearchBoxRenderer.prototype, "handleSearch", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], SearchBoxRenderer.prototype, "dispatchEvent", null);
    SearchBoxRenderer = __decorate([
        Renderer({
            type: 'search-box'
        }),
        __metadata("design:paramtypes", [Object, Object])
    ], SearchBoxRenderer);
    return SearchBoxRenderer;
})(React.Component));
