/**
 * DevExtreme (esm/__internal/ui/collection/m_item.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import Class from "../../../core/class";
import $ from "../../../core/renderer";
import {
    each
} from "../../../core/utils/iterator";
import {
    attachInstanceToElement,
    getInstanceByElement
} from "../../../core/utils/public_component";
var INVISIBLE_STATE_CLASS = "dx-state-invisible";
var DISABLED_STATE_CLASS = "dx-state-disabled";
var ITEM_CONTENT_PLACEHOLDER_CLASS = "dx-item-content-placeholder";
var forcibleWatcher = function(watchMethod, fn, callback) {
    var filteredCallback = function(value) {
        if (oldValue !== value) {
            callback(value, oldValue);
            oldValue = value
        }
    };
    var oldValue;
    return {
        dispose: watchMethod(fn, filteredCallback),
        force() {
            filteredCallback(fn())
        }
    }
};
var CollectionItem = Class.inherit({
    ctor($element, options, rawData) {
        this._$element = $element;
        this._options = options;
        this._rawData = rawData;
        attachInstanceToElement($element, this, this._dispose);
        this._render()
    },
    _render() {
        var $placeholder = $("<div>").addClass(ITEM_CONTENT_PLACEHOLDER_CLASS);
        this._$element.append($placeholder);
        this._watchers = [];
        this._renderWatchers()
    },
    _renderWatchers() {
        this._startWatcher("disabled", this._renderDisabled.bind(this));
        this._startWatcher("visible", this._renderVisible.bind(this))
    },
    _startWatcher(field, render) {
        var rawData = this._rawData;
        var exprGetter = this._options.fieldGetter(field);
        var watcher = forcibleWatcher(this._options.watchMethod(), () => exprGetter(rawData), (value, oldValue) => {
            this._dirty = true;
            render(value, oldValue)
        });
        this._watchers.push(watcher)
    },
    setDataField() {
        this._dirty = false;
        each(this._watchers, (_, watcher) => {
            watcher.force()
        });
        if (this._dirty) {
            return true
        }
    },
    _renderDisabled(value, oldValue) {
        this._$element.toggleClass(DISABLED_STATE_CLASS, !!value);
        this._$element.attr("aria-disabled", !!value);
        this._updateOwnerFocus(value)
    },
    _updateOwnerFocus(isDisabled) {
        var ownerComponent = this._options.owner;
        if (ownerComponent && isDisabled) {
            ownerComponent._resetItemFocus(this._$element)
        }
    },
    _renderVisible(value, oldValue) {
        this._$element.toggleClass(INVISIBLE_STATE_CLASS, void 0 !== value && !value)
    },
    _dispose() {
        each(this._watchers, (_, watcher) => {
            watcher.dispose()
        })
    }
});
CollectionItem.getInstance = function($element) {
    return getInstanceByElement($element, this)
};
export default CollectionItem;
