/**
 * DevExtreme (esm/__internal/grids/pivot_grid/field_chooser/dom.js)
 * Version: 22.1.13
 * Build date: Fri Feb 09 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../../../core/renderer";
import {
    getOuterWidth
} from "../../../../core/utils/size";
import {
    ATTRIBUTES,
    CLASSES,
    SORTABLE_CONST
} from "./const";

function getTreeViewItem($sourceItem) {
    return $sourceItem.clone().addClass(CLASSES.area.box).css("width", parseFloat(getOuterWidth($sourceItem)))
}

function getAreaBoxItemArray($sourceItem, target) {
    var $itemArray = $sourceItem.clone();
    if (target === SORTABLE_CONST.targets.drag) {
        $sourceItem.each((idx, sourceItem) => {
            var width = parseFloat(getOuterWidth(sourceItem));
            $itemArray.eq(idx).css("width", width);
            return true
        })
    }
    return $itemArray
}

function getDefaultItem($sourceItem) {
    return $("<div>").addClass(CLASSES.area.field).addClass(CLASSES.area.box).text($sourceItem.text())
}

function getItemArray($sourceItem, target) {
    var isAreaBox = $sourceItem.hasClass(CLASSES.area.box);
    var isTreeList = $sourceItem.attr(ATTRIBUTES.treeViewItem);
    if (isAreaBox) {
        return getAreaBoxItemArray($sourceItem, target)
    }
    if (isTreeList) {
        return getTreeViewItem($sourceItem)
    }
    return getDefaultItem($sourceItem)
}

function wrapItemsInFieldsContainer($itemArray) {
    var $wrappedTmpContainer = $("<div>");
    $itemArray.each((_, item) => {
        var $wrappedItem = $("<div>").addClass(CLASSES.pivotGrid.fieldsContainer).addClass(CLASSES.widget).append($(item));
        $wrappedTmpContainer.append($wrappedItem);
        return true
    });
    return $wrappedTmpContainer.children()
}
export function dragAndDropItemRender($sourceItem, target) {
    var $itemArray = getItemArray($sourceItem, target);
    if (target === SORTABLE_CONST.targets.drag) {
        return wrapItemsInFieldsContainer($itemArray)
    }
    return $itemArray
}
