/**
 * DevExtreme (esm/__internal/ui/m_box.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 registerComponent from "../../core/component_registrator";
import $ from "../../core/renderer";
import {
    extend
} from "../../core/utils/extend";
import {
    dasherize
} from "../../core/utils/inflector";
import {
    each
} from "../../core/utils/iterator";
import {
    normalizeStyleProp,
    setStyle,
    styleProp,
    stylePropPrefix
} from "../../core/utils/style";
import {
    isDefined
} from "../../core/utils/type";
import {
    hasWindow
} from "../../core/utils/window";
import CollectionWidget from "../../ui/collection/ui.collection_widget.edit";
import CollectionWidgetItem from "../../ui/collection/item";
var BOX_CLASS = "dx-box";
var BOX_FLEX_CLASS = "dx-box-flex";
var BOX_ITEM_CLASS = "dx-box-item";
var BOX_ITEM_DATA_KEY = "dxBoxItemData";
var SHRINK = 1;
var MINSIZE_MAP = {
    row: "minWidth",
    col: "minHeight"
};
var MAXSIZE_MAP = {
    row: "maxWidth",
    col: "maxHeight"
};
var FLEX_JUSTIFY_CONTENT_MAP = {
    start: "flex-start",
    end: "flex-end",
    center: "center",
    "space-between": "space-between",
    "space-around": "space-around"
};
var FLEX_ALIGN_ITEMS_MAP = {
    start: "flex-start",
    end: "flex-end",
    center: "center",
    stretch: "stretch"
};
var FLEX_DIRECTION_MAP = {
    row: "row",
    col: "column"
};
var setFlexProp = (element, prop, value) => {
    value = normalizeStyleProp(prop, value);
    element.style[styleProp(prop)] = value;
    if (!hasWindow()) {
        if ("" === value || !isDefined(value)) {
            return
        }
        var cssName = dasherize(prop);
        var styleExpr = "".concat(cssName, ": ").concat(value, ";");
        setStyle(element, styleExpr, false)
    }
};
class BoxItem extends CollectionWidgetItem {
    _renderVisible(value, oldValue) {
        super._renderVisible(value);
        if (isDefined(oldValue)) {
            this._options.fireItemStateChangedAction({
                name: "visible",
                state: value,
                oldState: oldValue
            })
        }
    }
}
class LayoutStrategy {
    constructor($element, option) {
        this._$element = $element;
        this._option = option
    }
    renderBox() {
        this._$element.css({
            display: "".concat(stylePropPrefix("flexDirection"), "flex")
        });
        setFlexProp(this._$element.get(0), "flexDirection", FLEX_DIRECTION_MAP[this._option("direction")])
    }
    renderAlign() {
        this._$element.css({
            justifyContent: this._normalizedAlign()
        })
    }
    _normalizedAlign() {
        var align = this._option("align");
        return align in FLEX_JUSTIFY_CONTENT_MAP ? FLEX_JUSTIFY_CONTENT_MAP[align] : align
    }
    renderCrossAlign() {
        this._$element.css({
            alignItems: this._normalizedCrossAlign()
        })
    }
    _normalizedCrossAlign() {
        var crossAlign = this._option("crossAlign");
        return crossAlign in FLEX_ALIGN_ITEMS_MAP ? FLEX_ALIGN_ITEMS_MAP[crossAlign] : crossAlign
    }
    renderItems($items) {
        var flexPropPrefix = stylePropPrefix("flexDirection");
        var direction = this._option("direction");
        each($items, (function() {
            var $item = $(this);
            var item = $item.data(BOX_ITEM_DATA_KEY);
            $item.css({
                display: "".concat(flexPropPrefix, "flex")
            }).css(MAXSIZE_MAP[direction], item.maxSize || "none").css(MINSIZE_MAP[direction], item.minSize || "0");
            setFlexProp($item.get(0), "flexBasis", item.baseSize || 0);
            setFlexProp($item.get(0), "flexGrow", item.ratio);
            setFlexProp($item.get(0), "flexShrink", isDefined(item.shrink) ? item.shrink : SHRINK);
            $item.children().each((_, itemContent) => {
                $(itemContent).css({
                    width: "auto",
                    height: "auto",
                    display: "".concat(stylePropPrefix("flexDirection"), "flex"),
                    flexBasis: 0
                });
                setFlexProp(itemContent, "flexGrow", 1);
                setFlexProp(itemContent, "flexDirection", $(itemContent)[0].style.flexDirection || "column")
            })
        }))
    }
}
class Box extends CollectionWidget {
    _getDefaultOptions() {
        return extend(super._getDefaultOptions(), {
            direction: "row",
            align: "start",
            crossAlign: "stretch",
            activeStateEnabled: false,
            focusStateEnabled: false,
            onItemStateChanged: void 0,
            _queue: void 0
        })
    }
    _itemClass() {
        return BOX_ITEM_CLASS
    }
    _itemDataKey() {
        return BOX_ITEM_DATA_KEY
    }
    _itemElements() {
        return this._itemContainer().children(this._itemSelector())
    }
    _init() {
        super._init();
        this.$element().addClass(BOX_FLEX_CLASS);
        this._initLayout();
        this._initBoxQueue()
    }
    _initLayout() {
        this._layout = new LayoutStrategy(this.$element(), this.option.bind(this))
    }
    _initBoxQueue() {
        this._queue = this.option("_queue") || []
    }
    _queueIsNotEmpty() {
        return this.option("_queue") ? false : !!this._queue.length
    }
    _pushItemToQueue($item, config) {
        this._queue.push({
            $item: $item,
            config: config
        })
    }
    _shiftItemFromQueue() {
        return this._queue.shift()
    }
    _initMarkup() {
        this.$element().addClass(BOX_CLASS);
        this._layout.renderBox();
        super._initMarkup();
        this._renderAlign();
        this._renderActions()
    }
    _renderActions() {
        this._onItemStateChanged = this._createActionByOption("onItemStateChanged")
    }
    _renderAlign() {
        this._layout.renderAlign();
        this._layout.renderCrossAlign()
    }
    _renderItems(items) {
        super._renderItems(items);
        while (this._queueIsNotEmpty()) {
            var item = this._shiftItemFromQueue();
            this._createComponent(item.$item, Box, extend({
                itemTemplate: this.option("itemTemplate"),
                itemHoldTimeout: this.option("itemHoldTimeout"),
                onItemHold: this.option("onItemHold"),
                onItemClick: this.option("onItemClick"),
                onItemContextMenu: this.option("onItemContextMenu"),
                onItemRendered: this.option("onItemRendered"),
                _queue: this._queue
            }, item.config))
        }
        this._layout.renderItems(this._itemElements())
    }
    _renderItemContent(args) {
        var $itemNode = args.itemData && args.itemData.node;
        if ($itemNode) {
            return this._renderItemContentByNode(args, $itemNode)
        }
        return super._renderItemContent(args)
    }
    _postprocessRenderItem(args) {
        var boxConfig = args.itemData.box;
        if (!boxConfig) {
            return
        }
        this._pushItemToQueue(args.itemContent, boxConfig)
    }
    _createItemByTemplate(itemTemplate, args) {
        if (args.itemData.box) {
            return itemTemplate.source ? itemTemplate.source() : $()
        }
        return super._createItemByTemplate(itemTemplate, args)
    }
    _itemOptionChanged(item, property, value, oldValue) {
        if ("visible" === property) {
            this._onItemStateChanged({
                name: property,
                state: value,
                oldState: false !== oldValue
            })
        }
        super._itemOptionChanged(item, property, value)
    }
    _optionChanged(args) {
        switch (args.name) {
            case "_queue":
            case "direction":
                this._invalidate();
                break;
            case "align":
                this._layout.renderAlign();
                break;
            case "crossAlign":
                this._layout.renderCrossAlign();
                break;
            default:
                super._optionChanged(args)
        }
    }
    _itemOptions() {
        var options = super._itemOptions();
        options.fireItemStateChangedAction = e => {
            this._onItemStateChanged(e)
        };
        return options
    }
}
Box.ItemClass = BoxItem;
registerComponent("dxBox", Box);
export default Box;
