import { __extends, __spreadArray } from "tslib";
import { BehaviorSubject } from 'rxjs';
import identity from '../../../utils/identity';
import { BasicModel } from './basic';
import { ValidateOption } from '../validate';
import { ModelRef } from './ref';
import { get, or, Some } from '../maybe';
import isNil from '../../../utils/isNil';
import uniqueId from '../../../utils/uniqueId';
import { pairwise, skip } from 'rxjs/operators';
import { createUnexpectedModelError } from '../error';
import { warningSubscribeValid, warningSubscribeValue } from '../warnings';
import { FIELD_ARRAY_ID, isModelRef, isModel } from './is';
import { createSentinelSubject } from './sentinel-subject';
var FieldArrayModel = (function (_super) {
    __extends(FieldArrayModel, _super);
    function FieldArrayModel(childBuilder, defaultValue) {
        var _this = _super.call(this, uniqueId('field-array-')) || this;
        _this.defaultValue = defaultValue;
        _this._displayName = 'FieldArrayModel';
        _this.owner = null;
        _this.invalidModels = new Set();
        _this.mapModelToSubscriptions = new Map();
        _this.normalizeBeforeSubmit = identity;
        _this._disposeChild = function (child) {
            _this._unsubscribeChild(child);
            child.owner = null;
        };
        _this._buildChild = function (child) {
            var model = _this.childFactory(child);
            _this._subscribeChild(model);
            return model;
        };
        _this.childFactory = childBuilder
            ? function (defaultValue) {
                var child = childBuilder.build(Some(defaultValue));
                return _this._linkChild(child);
            }
            : function (defaultValue) {
                return new ModelRef(null, Some(defaultValue), _this);
            };
        var children = _this.defaultValue.map(_this._buildChild);
        _this.children$ = new BehaviorSubject(children);
        return _this;
    }
    Object.defineProperty(FieldArrayModel.prototype, "value", {
        get: function () {
            if (this._value$) {
                return this._value$.value;
            }
            return this.getRawValue();
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FieldArrayModel.prototype, "value$", {
        get: function () {
            return this._getValue$(true);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FieldArrayModel.prototype, "valid$", {
        get: function () {
            return this._getValid$(true);
        },
        enumerable: false,
        configurable: true
    });
    FieldArrayModel.prototype._getValue$ = function (shouldWarn) {
        if (shouldWarn === void 0) { shouldWarn = false; }
        warningSubscribeValue(shouldWarn, this._displayName);
        if (!this._value$) {
            this._initValue$();
        }
        return this._value$;
    };
    FieldArrayModel.prototype._getValid$ = function (shouldWarn) {
        if (shouldWarn === void 0) { shouldWarn = false; }
        warningSubscribeValid(shouldWarn, this._displayName);
        if (!this._valid$) {
            this._initValid$();
        }
        return this._valid$;
    };
    FieldArrayModel.prototype.reset = function () {
        var _this = this;
        var children = or(this.initialValue, function () { return _this.defaultValue; }).map(this._buildChild);
        this.children$.next(children);
    };
    FieldArrayModel.prototype.clear = function () {
        this.initialValue = undefined;
        var children = this.defaultValue.map(this._buildChild);
        this.children$.next(children);
    };
    FieldArrayModel.prototype.clearError = function () {
        this.error$.next(null);
        var children = this.children;
        var length = children.length;
        for (var i = 0; i < length; i++) {
            var element = children[i];
            element.clearError();
        }
    };
    Object.defineProperty(FieldArrayModel.prototype, "children", {
        get: function () {
            return this.children$.getValue();
        },
        enumerable: false,
        configurable: true
    });
    FieldArrayModel.prototype.get = function (index) {
        return this.children[index];
    };
    FieldArrayModel.prototype.getRawValue = function () {
        return this._getValue(function (model) { return model.getRawValue(); });
    };
    FieldArrayModel.prototype.getSubmitValue = function () {
        var value = this._getValue(function (model) { return model.getSubmitValue(); });
        return this.normalizeBeforeSubmit(value);
    };
    FieldArrayModel.prototype.patchValue = function (value) {
        var children = this.children$.getValue();
        for (var i = 0; i < value.length; i += 1) {
            if (i >= children.length) {
                break;
            }
            var item = value[i];
            var model = children[i];
            if (isModelRef(model)) {
                var m = model.getModel();
                m && m.patchValue(item);
            }
            else if (isModel(model)) {
                model.patchValue(item);
            }
        }
        if (value.length <= children.length) {
            this.splice(value.length, children.length - value.length);
            return;
        }
        for (var i = children.length; i < value.length; i += 1) {
            var item = value[i];
            this.push(item);
        }
    };
    FieldArrayModel.prototype.initialize = function (values) {
        this.initialValue = Some(values);
        var children = values.map(this._buildChild);
        this.children$.next(children);
    };
    FieldArrayModel.prototype.push = function () {
        var _this = this;
        var items = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            items[_i] = arguments[_i];
        }
        var nextChildren = this.children$.getValue().concat(items.map(function (item) {
            return isModel(item)
                ? _this._linkChild(item)
                : _this._buildChild(item);
        }));
        this.children$.next(nextChildren);
        return nextChildren.length;
    };
    FieldArrayModel.prototype.pop = function () {
        var children = this.children$.getValue().slice();
        var child = children.pop();
        child && this._disposeChild(child);
        this.children$.next(children);
        return child;
    };
    FieldArrayModel.prototype.shift = function () {
        var children = this.children$.getValue().slice();
        var child = children.shift();
        child && this._disposeChild(child);
        this.children$.next(children);
        return child;
    };
    FieldArrayModel.prototype.unshift = function () {
        var _this = this;
        var items = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            items[_i] = arguments[_i];
        }
        var nextChildren = items.map(function (item) {
            return isModel(item)
                ? _this._linkChild(item)
                : _this._buildChild(item);
        }).concat(this.children$.getValue());
        this.children$.next(nextChildren);
        return nextChildren.length;
    };
    FieldArrayModel.prototype.splice = function (start, deleteCount) {
        var _this = this;
        if (deleteCount === void 0) { deleteCount = 0; }
        var items = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            items[_i - 2] = arguments[_i];
        }
        var children = this.children$.getValue().slice();
        var insertedChildren = items.map(function (item) {
            return isModel(item)
                ? _this._linkChild(item)
                : _this._buildChild(item);
        });
        var removedChildren = children.splice.apply(children, __spreadArray([start,
            deleteCount], insertedChildren));
        removedChildren.forEach(this._disposeChild);
        this.children$.next(children);
        return removedChildren;
    };
    FieldArrayModel.prototype.filter = function (predicate) {
        var _this = this;
        var children = this.children$.getValue().filter(function (item, index, array) {
            var keep = predicate(item, index, array);
            if (!keep) {
                _this._disposeChild(item);
            }
            return keep;
        });
        this.children$.next(children);
        return this;
    };
    FieldArrayModel.prototype.sort = function (compareFn) {
        var children = this.children$.getValue().slice().sort(compareFn);
        this.children$.next(children);
        return this;
    };
    FieldArrayModel.prototype.validate = function (option) {
        if (option === void 0) { option = ValidateOption.Default; }
        if (option & ValidateOption.IncludeChildrenRecursively) {
            var childOption_1 = option | ValidateOption.StopPropagation;
            return Promise.all(this.children$
                .getValue()
                .map(function (it) { return it.validate(childOption_1); })
                .concat(this.triggerValidate(option)));
        }
        return this.triggerValidate(option);
    };
    FieldArrayModel.prototype.pristine = function () {
        var children = this.children$.getValue();
        for (var i = 0; i < children.length; i += 1) {
            var child = children[i];
            if (child.dirty()) {
                return false;
            }
        }
        return true;
    };
    FieldArrayModel.prototype.dirty = function () {
        return !this.pristine();
    };
    FieldArrayModel.prototype.touched = function () {
        var children = this.children$.getValue();
        for (var i = 0; i < children.length; i += 1) {
            var child = children[i];
            if (child.touched()) {
                return true;
            }
        }
        return false;
    };
    FieldArrayModel.prototype.dispose = function () {
        var _this = this;
        var _a, _b;
        _super.prototype.dispose.call(this);
        this.children.forEach(function (child) {
            _this._unsubscribeChild(child);
            child.dispose();
        });
        this.children$.next([]);
        this.children$.complete();
        (_a = this._value$) === null || _a === void 0 ? void 0 : _a.complete();
        (_b = this._valid$) === null || _b === void 0 ? void 0 : _b.complete();
        this._valid$ = createSentinelSubject(this._displayName, false);
        this._value$ = createSentinelSubject(this._displayName, []);
        this.children$ = createSentinelSubject(this._displayName, []);
    };
    FieldArrayModel.prototype._linkChild = function (child) {
        child.owner = this;
        return child;
    };
    FieldArrayModel.prototype._initValue$ = function () {
        var _this = this;
        var value$ = new BehaviorSubject(this.getRawValue());
        this._value$ = value$;
        this.children$.pipe(skip(1)).subscribe(function () {
            value$.next(_this.getRawValue());
        });
        for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
            var child = _a[_i];
            this._subscribeChild(child);
        }
        if (!this._valid$) {
            this._initUnsubscribeChild();
        }
    };
    FieldArrayModel.prototype._initValid$ = function () {
        var _this = this;
        var valid$ = new BehaviorSubject(isNil(this.error));
        this._valid$ = valid$;
        var $ = this.error$.subscribe(function (maybeError) {
            var selfValid = isNil(maybeError);
            valid$.next(selfValid && !_this.invalidModels.size);
        });
        this.mapModelToSubscriptions.set(this, [$]);
        this.children$.pipe(skip(1)).subscribe(function () {
            valid$.next(isNil(_this.error) && !_this.invalidModels.size);
        });
        for (var _i = 0, _a = this.children; _i < _a.length; _i++) {
            var child = _a[_i];
            this._subscribeChild(child);
        }
        if (!this._value$) {
            this._initUnsubscribeChild();
        }
    };
    FieldArrayModel.prototype._initUnsubscribeChild = function () {
        var _this = this;
        this.children$.pipe(pairwise()).subscribe(function (_a) {
            var prev = _a[0], current = _a[1];
            for (var _i = 0, prev_1 = prev; _i < prev_1.length; _i++) {
                var child = prev_1[_i];
                if (!current.includes(child)) {
                    _this._unsubscribeChild(child);
                }
            }
        });
    };
    FieldArrayModel.prototype._getValue = function (getter) {
        return this.children$.getValue().map(function (child) {
            if (isModelRef(child)) {
                var model = child.getModel();
                return isModel(model)
                    ? getter(model)
                    : get(child.initialValue);
            }
            else if (isModel(child)) {
                return getter(child);
            }
            throw createUnexpectedModelError(child);
        });
    };
    FieldArrayModel.prototype._subscribeChild = function (child) {
        var _this = this;
        var _a = this, _valid$ = _a._valid$, _value$ = _a._value$, mapModelToSubscriptions = _a.mapModelToSubscriptions;
        if (_valid$ || _value$) {
            if (isModelRef(child)) {
                var model = child.getModel();
                if (isModel(model)) {
                    this._subscribeChildModel(model);
                }
                var $ = child.model$.pipe(pairwise()).subscribe(function (_a) {
                    var prev = _a[0], current = _a[1];
                    prev && _this._unsubscribeChild(prev);
                    if (isModel(current)) {
                        _this._subscribeChildModel(current);
                    }
                });
                mapModelToSubscriptions.set(child, [$]);
            }
            else if (isModel(child)) {
                this._subscribeChildModel(child);
            }
        }
    };
    FieldArrayModel.prototype._subscribeChildModel = function (model) {
        var _this = this;
        var _a = this, error$ = _a.error$, _valid$ = _a._valid$, _value$ = _a._value$, invalidModels = _a.invalidModels;
        if (_valid$) {
            this._subscribeObservable(model, model._getValid$(), function (valid) {
                if (valid) {
                    invalidModels.delete(model);
                }
                else {
                    invalidModels.add(model);
                }
                _valid$.next(!invalidModels.size && isNil(error$.value));
            });
        }
        if (_value$) {
            this._subscribeObservable(model, model._getValue$(), function (childValue) {
                var index = _this.children.findIndex(function (it) {
                    if (isModelRef(it)) {
                        return it.getModel() === model;
                    }
                    else if (isModel(it)) {
                        return it === model;
                    }
                    else {
                        throw createUnexpectedModelError(it);
                    }
                });
                var copy = __spreadArray([], _value$.value);
                copy.splice(index, 1, childValue);
                _value$.next(copy);
            }, true);
        }
    };
    FieldArrayModel.prototype._subscribeObservable = function (model, observable, observer, skipFirst) {
        var mapModelToSubscriptions = this.mapModelToSubscriptions;
        var $ = (skipFirst ? observable.pipe(skip(1)) : observable).subscribe(observer);
        var subs = mapModelToSubscriptions.get(model);
        if (subs) {
            subs.push($);
        }
        else {
            mapModelToSubscriptions.set(model, [$]);
        }
    };
    FieldArrayModel.prototype._unsubscribeChild = function (child) {
        var model = null;
        if (isModel(child)) {
            this.invalidModels.delete(child);
            model = child;
        }
        else if (isModelRef(child)) {
            model = child.getModel();
        }
        this._unsubscribeModel(child);
        if (model) {
            this._unsubscribeModel(model);
        }
    };
    FieldArrayModel.prototype._unsubscribeModel = function (model) {
        var subs = this.mapModelToSubscriptions.get(model);
        subs === null || subs === void 0 ? void 0 : subs.forEach(function (sub) { return sub.unsubscribe(); });
        this.mapModelToSubscriptions.delete(model);
        if (isModel(model)) {
            this.invalidModels.delete(model);
        }
    };
    return FieldArrayModel;
}(BasicModel));
FieldArrayModel.prototype[FIELD_ARRAY_ID] = true;
export { FieldArrayModel };
