/**
 * DevExtreme (esm/renovation/utils/plugin/placeholder_item.js)
 * Version: 22.1.3 (build 22180-0310)
 * Build date: Wed Jun 29 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
var _excluded = ["children", "column", "componentDeps", "componentTypes", "index"];
import {
    createFragment,
    createComponentVNode
} from "inferno";
import {
    Fragment
} from "inferno";
import {
    InfernoEffect,
    InfernoComponent
} from "@devextreme/runtime/inferno";
import {
    PluginsContext
} from "./context";
import {
    PlaceholderItemRenderer
} from "./placeholder_item_renderer";
export var viewFunction = _ref => {
    var {
        args: args,
        currentTemplate: currentTemplate,
        props: {
            children: children,
            column: column,
            componentDeps: componentDeps,
            componentTypes: componentTypes,
            index: index
        }
    } = _ref;
    return createFragment(currentTemplate ? createComponentVNode(2, PlaceholderItemRenderer, {
        deps: args,
        column: column,
        currentTemplate: currentTemplate,
        baseTemplate: () => createComponentVNode(2, PlaceholderItem, {
            componentTypes: componentTypes,
            componentDeps: componentDeps,
            column: column,
            index: index + 1
        })
    }) : createFragment(children, 0), 0)
};
export var PlaceholderItemProps = {
    componentTypes: Object.freeze([]),
    componentDeps: Object.freeze([]),
    index: 0
};
export class PlaceholderItem extends InfernoComponent {
    constructor(props) {
        super(props);
        this.__getterCache = {};
        this.state = {
            args: []
        };
        this.updateArgs = this.updateArgs.bind(this);
        this.getArgs = this.getArgs.bind(this)
    }
    get plugins() {
        if ("PluginsContext" in this.context) {
            return this.context.PluginsContext
        }
        return PluginsContext
    }
    createEffects() {
        return [new InfernoEffect(this.updateArgs, [this.props.componentDeps, this.props.index, this.plugins])]
    }
    updateEffects() {
        var _this$_effects$;
        null === (_this$_effects$ = this._effects[0]) || void 0 === _this$_effects$ ? void 0 : _this$_effects$.update([this.props.componentDeps, this.props.index, this.plugins])
    }
    updateArgs() {
        var disposers = this.componentDeps.map(entity => this.plugins.watch(entity, () => {
            this.setState(__state_argument => ({
                args: this.getArgs()
            }))
        }));
        return () => {
            disposers.forEach(disposer => disposer())
        }
    }
    getArgs() {
        return this.componentDeps.map(entity => this.plugins.getValue(entity))
    }
    get currentTemplate() {
        if (this.componentDeps.every(entity => this.plugins.hasValue(entity))) {
            return this.props.componentTypes[this.props.index]
        }
        return null
    }
    get componentDeps() {
        if (void 0 !== this.__getterCache.componentDeps) {
            return this.__getterCache.componentDeps
        }
        return this.__getterCache.componentDeps = (() => {
            var _this$props$component;
            return null !== (_this$props$component = this.props.componentDeps[this.props.index]) && void 0 !== _this$props$component ? _this$props$component : []
        })()
    }
    get restAttributes() {
        var _this$props = this.props,
            restProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
        return restProps
    }
    componentWillUpdate(nextProps, nextState, context) {
        super.componentWillUpdate();
        if (this.props.componentDeps !== nextProps.componentDeps || this.props.index !== nextProps.index) {
            this.__getterCache.componentDeps = void 0
        }
    }
    render() {
        var props = this.props;
        return viewFunction({
            props: _extends({}, props),
            args: this.state.args,
            plugins: this.plugins,
            getArgs: this.getArgs,
            currentTemplate: this.currentTemplate,
            componentDeps: this.componentDeps,
            restAttributes: this.restAttributes
        })
    }
}
PlaceholderItem.defaultProps = PlaceholderItemProps;
