/**
 * DevExtreme (esm/renovation/component_wrapper/common/template_wrapper.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import _extends from "@babel/runtime/helpers/esm/extends";
var _excluded = ["isEqual"];
import {
    InfernoComponent,
    InfernoEffect
} from "@devextreme/runtime/inferno";
import {
    findDOMfromVNode
} from "inferno";
import {
    replaceWith
} from "../../../core/utils/dom";
import {
    shallowEquals
} from "../../utils/shallow_equals";
import $ from "../../../core/renderer";
import domAdapter from "../../../core/dom_adapter";
import {
    getPublicElement
} from "../../../core/element";
import {
    isDefined
} from "../../../core/utils/type";

function isDxElementWrapper(element) {
    return !!element.toArray
}
export function buildTemplateArgs(model, template) {
    var _model$data;
    var args = {
        template: template,
        model: _extends({}, model)
    };
    var _ref = null !== (_model$data = model.data) && void 0 !== _model$data ? _model$data : {},
        {
            isEqual: isEqual
        } = _ref,
        data = _objectWithoutPropertiesLoose(_ref, _excluded);
    if (isEqual) {
        args.model.data = data;
        args.isEqual = isEqual
    }
    return args
}

function renderTemplateContent(props, container) {
    var _props$model;
    var {
        data: data,
        index: index
    } = null !== (_props$model = props.model) && void 0 !== _props$model ? _props$model : {
        data: {}
    };
    if (data) {
        Object.keys(data).forEach(name => {
            if (data[name] && domAdapter.isNode(data[name])) {
                data[name] = getPublicElement($(data[name]))
            }
        })
    }
    var rendered = props.template.render(_extends({
        container: container,
        transclude: props.transclude
    }, {
        renovated: props.renovated
    }, !props.transclude ? {
        model: data
    } : {}, !props.transclude && Number.isFinite(index) ? {
        index: index
    } : {}));
    if (void 0 === rendered) {
        return []
    }
    return isDxElementWrapper(rendered) ? rendered.toArray() : [$(rendered).get(0)]
}

function removeDifferentElements(oldChildren, newChildren) {
    newChildren.forEach(newElement => {
        var hasOldChild = !!oldChildren.find(oldElement => newElement === oldElement);
        if (!hasOldChild && newElement.parentNode) {
            $(newElement).remove()
        }
    })
}
export class TemplateWrapper extends InfernoComponent {
    constructor(props) {
        super(props);
        this.renderTemplate = this.renderTemplate.bind(this)
    }
    renderTemplate() {
        var node = findDOMfromVNode(this.$LI, true);
        if (!(null !== node && void 0 !== node && node.parentNode)) {
            return () => {}
        }
        var container = node.parentNode;
        var $container = $(container);
        var $oldContainerContent = $container.contents().toArray();
        var content = renderTemplateContent(this.props, getPublicElement($container));
        replaceWith($(node), $(content));
        return () => {
            var $actualContainerContent = $(container).contents().toArray();
            removeDifferentElements($oldContainerContent, $actualContainerContent);
            container.appendChild(node)
        }
    }
    shouldComponentUpdate(nextProps) {
        var {
            model: model,
            template: template
        } = this.props;
        var {
            isEqual: isEqual,
            model: nextModel,
            template: nextTemplate
        } = nextProps;
        var equalityComparer = null !== isEqual && void 0 !== isEqual ? isEqual : shallowEquals;
        if (template !== nextTemplate) {
            return true
        }
        if (!isDefined(model) || !isDefined(nextModel)) {
            return model !== nextModel
        }
        var {
            data: data,
            index: index
        } = model;
        var {
            data: nextData,
            index: nextIndex
        } = nextModel;
        if (index !== nextIndex) {
            return true
        }
        return !equalityComparer(data, nextData)
    }
    createEffects() {
        return [new InfernoEffect(this.renderTemplate, [this.props.template, this.props.model])]
    }
    updateEffects() {
        this._effects[0].update([this.props.template, this.props.model])
    }
    componentWillUnmount() {}
    render() {
        return null
    }
}
