/**
 * DevExtreme (ui/form/ui.form.utils.js)
 * Version: 20.2.9
 * Build date: Tue Aug 17 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.getLabelWidthByText = getLabelWidthByText;
exports.renderLabel = renderLabel;
exports.renderHelpText = renderHelpText;
exports.convertAlignmentToJustifyContent = convertAlignmentToJustifyContent;
exports.convertAlignmentToTextAlign = convertAlignmentToTextAlign;
exports.adjustContainerAsButtonItem = adjustContainerAsButtonItem;
exports.adjustEditorContainer = adjustEditorContainer;
exports.renderTemplateTo = renderTemplateTo;
exports.renderComponentTo = renderComponentTo;
exports.getItemPath = exports.isFullPathContainsTabs = exports.tryGetTabPath = exports.getOptionNameFromFullName = exports.getFullOptionName = exports.isExpectedItem = exports.getTextWithoutSpaces = exports.concatPaths = exports.createItemPathByIndex = void 0;
var _renderer = _interopRequireDefault(require("../../core/renderer"));
var _ui = _interopRequireDefault(require("../widget/ui.errors"));
var _type = require("../../core/utils/type");
var _constants = require("./constants");

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        "default": obj
    }
}

function _toConsumableArray(arr) {
    return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(o, minLen) {
    if (!o) {
        return
    }
    if ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _iterableToArray(iter) {
    if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
        return Array.from(iter)
    }
}

function _arrayWithoutHoles(arr) {
    if (Array.isArray(arr)) {
        return _arrayLikeToArray(arr)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || len > arr.length) {
        len = arr.length
    }
    for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i]
    }
    return arr2
}
var createItemPathByIndex = function(index, isTabs) {
    return "".concat(isTabs ? "tabs" : "items", "[").concat(index, "]")
};
exports.createItemPathByIndex = createItemPathByIndex;
var concatPaths = function(path1, path2) {
    if ((0, _type.isDefined)(path1) && (0, _type.isDefined)(path2)) {
        return "".concat(path1, ".").concat(path2)
    }
    return path1 || path2
};
exports.concatPaths = concatPaths;
var getTextWithoutSpaces = function(text) {
    return text ? text.replace(/\s/g, "") : void 0
};
exports.getTextWithoutSpaces = getTextWithoutSpaces;
var isExpectedItem = function(item, fieldName) {
    return item && (item.dataField === fieldName || item.name === fieldName || getTextWithoutSpaces(item.title) === fieldName || "group" === item.itemType && getTextWithoutSpaces(item.caption) === fieldName)
};
exports.isExpectedItem = isExpectedItem;
var getFullOptionName = function(path, optionName) {
    return "".concat(path, ".").concat(optionName)
};
exports.getFullOptionName = getFullOptionName;
var getOptionNameFromFullName = function(fullName) {
    var parts = fullName.split(".");
    return parts[parts.length - 1].replace(/\[\d+]/, "")
};
exports.getOptionNameFromFullName = getOptionNameFromFullName;
var tryGetTabPath = function(fullPath) {
    var pathParts = fullPath.split(".");
    var resultPathParts = _toConsumableArray(pathParts);
    for (var i = pathParts.length - 1; i >= 0; i--) {
        if (isFullPathContainsTabs(pathParts[i])) {
            return resultPathParts.join(".")
        }
        resultPathParts.splice(i, 1)
    }
    return ""
};
exports.tryGetTabPath = tryGetTabPath;
var isFullPathContainsTabs = function(fullPath) {
    return fullPath.indexOf("tabs") > -1
};
exports.isFullPathContainsTabs = isFullPathContainsTabs;
var getItemPath = function getItemPath(items, item, isTabs) {
    var index = items.indexOf(item);
    if (index > -1) {
        return createItemPathByIndex(index, isTabs)
    }
    for (var i = 0; i < items.length; i++) {
        var targetItem = items[i];
        var tabOrGroupItems = targetItem.tabs || targetItem.items;
        if (tabOrGroupItems) {
            var itemPath = getItemPath(tabOrGroupItems, item, targetItem.tabs);
            if (itemPath) {
                return concatPaths(createItemPathByIndex(i, isTabs), itemPath)
            }
        }
    }
};
exports.getItemPath = getItemPath;

function getLabelWidthByText(renderLabelOptions) {
    var $hiddenContainer = (0, _renderer.default)("<div>").addClass(_constants.WIDGET_CLASS).addClass(_constants.HIDDEN_LABEL_CLASS).appendTo("body");
    var $label = renderLabel(renderLabelOptions).appendTo($hiddenContainer);
    var labelTextElement = $label.find("." + _constants.FIELD_ITEM_LABEL_TEXT_CLASS)[0];
    var result = labelTextElement.offsetWidth;
    $hiddenContainer.remove();
    return result
}

function renderLabel(_ref) {
    var text = _ref.text,
        id = _ref.id,
        location = _ref.location,
        alignment = _ref.alignment,
        _ref$labelID = _ref.labelID,
        labelID = void 0 === _ref$labelID ? null : _ref$labelID,
        _ref$markOptions = _ref.markOptions,
        markOptions = void 0 === _ref$markOptions ? {} : _ref$markOptions;
    if (!(0, _type.isDefined)(text) || text.length <= 0) {
        return null
    }
    return (0, _renderer.default)("<label>").addClass(_constants.FIELD_ITEM_LABEL_CLASS + " " + _constants.FIELD_ITEM_LABEL_LOCATION_CLASS + location).attr("for", id).attr("id", labelID).css("textAlign", alignment).append((0, _renderer.default)("<span>").addClass(_constants.FIELD_ITEM_LABEL_CONTENT_CLASS).append((0, _renderer.default)("<span>").addClass(_constants.FIELD_ITEM_LABEL_TEXT_CLASS).text(text), _renderLabelMark(markOptions)))
}

function renderHelpText(helpText, helpID) {
    return (0, _renderer.default)("<div>").addClass(_constants.FIELD_ITEM_HELP_TEXT_CLASS).attr("id", helpID).text(helpText)
}

function _renderLabelMark(_ref2) {
    var isRequiredMark = _ref2.isRequiredMark,
        requiredMark = _ref2.requiredMark,
        isOptionalMark = _ref2.isOptionalMark,
        optionalMark = _ref2.optionalMark;
    if (!isRequiredMark && !isOptionalMark) {
        return null
    }
    return (0, _renderer.default)("<span>").addClass(isRequiredMark ? _constants.FIELD_ITEM_REQUIRED_MARK_CLASS : _constants.FIELD_ITEM_OPTIONAL_MARK_CLASS).text(String.fromCharCode(160) + (isRequiredMark ? requiredMark : optionalMark))
}

function convertAlignmentToJustifyContent(verticalAlignment) {
    switch (verticalAlignment) {
        case "center":
            return "center";
        case "bottom":
            return "flex-end";
        default:
            return "flex-start"
    }
}

function convertAlignmentToTextAlign(horizontalAlignment) {
    return (0, _type.isDefined)(horizontalAlignment) ? horizontalAlignment : "right"
}

function adjustContainerAsButtonItem(_ref3) {
    var $container = _ref3.$container,
        justifyContent = _ref3.justifyContent,
        textAlign = _ref3.textAlign,
        cssItemClass = _ref3.cssItemClass,
        targetColIndex = _ref3.targetColIndex;
    $container.addClass(_constants.FIELD_BUTTON_ITEM_CLASS).css("textAlign", textAlign).addClass(_constants.FIELD_ITEM_CLASS).addClass(cssItemClass).addClass((0, _type.isDefined)(targetColIndex) ? "dx-col-" + targetColIndex : "");
    $container.parent().css("justifyContent", justifyContent)
}

function adjustEditorContainer(_ref4) {
    var $container = _ref4.$container,
        labelLocation = _ref4.labelLocation;
    var locationClassSuffix = {
        right: "left",
        left: "right",
        top: "bottom"
    };
    $container.addClass(_constants.FIELD_ITEM_CONTENT_CLASS).addClass(_constants.FIELD_ITEM_CONTENT_LOCATION_CLASS + locationClassSuffix[labelLocation])
}

function renderTemplateTo(_ref5) {
    var $container = _ref5.$container,
        template = _ref5.template,
        templateOptions = _ref5.templateOptions;
    template.render({
        model: templateOptions,
        container: $container
    })
}

function renderComponentTo(_ref6) {
    var $container = _ref6.$container,
        createComponentCallback = _ref6.createComponentCallback,
        componentType = _ref6.componentType,
        componentOptions = _ref6.componentOptions,
        helpID = _ref6.helpID,
        labelID = _ref6.labelID,
        isRequired = _ref6.isRequired;
    var $div = (0, _renderer.default)("<div>").appendTo($container);
    try {
        var result = createComponentCallback($div, componentType, componentOptions);
        result.setAria("describedby", helpID);
        result.setAria("labelledby", labelID);
        result.setAria("required", isRequired);
        return result
    } catch (e) {
        _ui.default.log("E1035", e.message)
    }
}
