"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var cx = require("classnames");
var Options_1 = require("./Options");
var Select_1 = require("../../components/Select");
;
var ChainedSelectControl = /** @class */ (function (_super) {
    tslib_1.__extends(ChainedSelectControl, _super);
    function ChainedSelectControl(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            stack: []
        };
        _this.handleChange = _this.handleChange.bind(_this);
        _this.loadMore = _this.loadMore.bind(_this);
        return _this;
    }
    ChainedSelectControl.prototype.componentDidMount = function () {
        var formInited = this.props.formInited;
        formInited ? this.loadMore() : this.props.addHook(this.loadMore, 'init');
    };
    ChainedSelectControl.prototype.componentWillReceiveProps = function (nextProps) {
        var props = this.props;
        if (props.options !== nextProps.options) {
            this.setState({
                stack: []
            });
        }
    };
    ChainedSelectControl.prototype.componentDidUpdate = function (prevProps) {
        var props = this.props;
        if (props.value !== prevProps.value) {
            this.loadMore();
        }
    };
    ChainedSelectControl.prototype.loadMore = function () {
        var _this = this;
        var _a = this.props, value = _a.value, delimiter = _a.delimiter, onChange = _a.onChange, joinValues = _a.joinValues, extractValue = _a.extractValue, source = _a.source, data = _a.data, env = _a.env;
        var arr = Array.isArray(value) ? value.concat() : value && typeof value === 'string' ? value.split(delimiter || ',') : [];
        var idx = 0;
        var len = this.state.stack.length;
        while (idx < len && arr[idx] && this.state.stack[idx].parentId == ((joinValues || extractValue) ? arr[idx] : arr[idx].value)) {
            idx++;
        }
        if (!arr[idx] || !env || !source) {
            return;
        }
        var parentId = (joinValues || extractValue) ? arr[idx] : arr[idx].value;
        var stack = this.state.stack.concat();
        stack.splice(idx, stack.length - idx);
        stack.push({
            parentId: parentId,
            loading: true,
            options: []
        });
        this.setState({
            stack: stack
        }, function () {
            env
                .fetcher(source, tslib_1.__assign({}, data, { value: arr, level: idx + 1, parentId: parentId, parent: arr[idx] }))
                .then(function (ret) {
                var stack = _this.state.stack.concat();
                var remoteValue = ret.data ? ret.data.value : undefined;
                var options = ret.data && ret.data.options || ret.data;
                stack.splice(idx, stack.length - idx);
                if (typeof remoteValue !== 'undefined') {
                    arr.splice(idx + 1, value.length - idx - 1);
                    arr.push(remoteValue);
                    onChange(joinValues ? arr.join(delimiter || ',') : arr);
                }
                stack.push({
                    options: options,
                    parentId: parentId,
                    loading: false,
                    visible: !!options
                });
                _this.setState({
                    stack: stack
                }, _this.loadMore);
            })
                .catch(function (e) {
                env.notify('error', e.message);
            });
        });
    };
    ChainedSelectControl.prototype.handleChange = function (index, currentValue) {
        var _a = this.props, value = _a.value, delimiter = _a.delimiter, onChange = _a.onChange, joinValues = _a.joinValues, extractValue = _a.extractValue;
        var arr = Array.isArray(value) ? value.concat() : value && typeof value === 'string' ? value.split(delimiter || ',') : [];
        arr.splice(index, arr.length - index);
        arr.push(joinValues ? currentValue.value : currentValue);
        onChange(joinValues ? arr.join(delimiter || ',') : extractValue ? arr.map(function (item) { return item.value || item; }) : arr);
    };
    ChainedSelectControl.prototype.render = function () {
        var _this = this;
        var _a = this.props, options = _a.options, ns = _a.classPrefix, className = _a.className, inline = _a.inline, loading = _a.loading, value = _a.value, delimiter = _a.delimiter, joinValues = _a.joinValues, extractValue = _a.extractValue, multiple = _a.multiple, rest = tslib_1.__rest(_a, ["options", "classPrefix", "className", "inline", "loading", "value", "delimiter", "joinValues", "extractValue", "multiple"]);
        var arr = Array.isArray(value) ? value.concat() : value && typeof value === 'string' ? value.split(delimiter || ',') : [];
        return (React.createElement("div", { className: cx(ns + "ChainedSelectControl", className) },
            React.createElement(Select_1.default, tslib_1.__assign({}, rest, { classPrefix: ns, key: "base", options: options, value: arr[0], onChange: this.handleChange.bind(this, 0), loading: loading, inline: true })),
            this.state.stack.map(function (_a, index) {
                var options = _a.options, loading = _a.loading, visible = _a.visible;
                return visible === false ? null : (React.createElement(Select_1.default, tslib_1.__assign({}, rest, { classPrefix: ns, key: "x-" + (index + 1), options: options, value: arr[index + 1], onChange: _this.handleChange.bind(_this, index + 1), loading: loading, inline: true })));
            })));
    };
    ChainedSelectControl.defaultProps = {
        clearable: false,
        searchable: false,
        multiple: true
    };
    return ChainedSelectControl;
}(React.Component));
exports.default = ChainedSelectControl;
var ChainedSelectControlRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(ChainedSelectControlRenderer, _super);
    function ChainedSelectControlRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    ChainedSelectControlRenderer = tslib_1.__decorate([
        Options_1.OptionsControl({
            type: 'chained-select',
            sizeMutable: false
        })
    ], ChainedSelectControlRenderer);
    return ChainedSelectControlRenderer;
}(ChainedSelectControl));
exports.ChainedSelectControlRenderer = ChainedSelectControlRenderer;
;
//# sourceMappingURL=./renderers/Form/ChainedSelect.js.map
