/**
 * DevExtreme (esm/ui/form/components/field_item.js)
 * Version: 23.1.6 (build 23316-0110)
 * Build date: Sun Nov 12 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../../core/renderer";
import eventsEngine from "../../../events/core/events_engine";
import {
    name as clickEventName
} from "../../../events/click";
import {
    getPublicElement
} from "../../../core/element";
import {
    captionize
} from "../../../core/utils/inflector";
import {
    format
} from "../../../core/utils/string";
import {
    isMaterial
} from "../../themes";
import errors from "../../widget/ui.errors";
import Validator from "../../validator";
import {
    FIELD_ITEM_CONTENT_CLASS
} from "../constants";
export var FLEX_LAYOUT_CLASS = "dx-flex-layout";
export var FIELD_ITEM_OPTIONAL_CLASS = "dx-field-item-optional";
export var FIELD_ITEM_REQUIRED_CLASS = "dx-field-item-required";
export var FIELD_ITEM_CONTENT_WRAPPER_CLASS = "dx-field-item-content-wrapper";
export var FIELD_ITEM_CONTENT_LOCATION_CLASS = "dx-field-item-content-location-";
export var FIELD_ITEM_LABEL_ALIGN_CLASS = "dx-field-item-label-align";
export var FIELD_ITEM_HELP_TEXT_CLASS = "dx-field-item-help-text";
export var LABEL_VERTICAL_ALIGNMENT_CLASS = "dx-label-v-align";
export var LABEL_HORIZONTAL_ALIGNMENT_CLASS = "dx-label-h-align";
export var TOGGLE_CONTROLS_PADDING_CLASS = "dx-toggle-controls-paddings";
import {
    renderLabel
} from "./label";
var TEMPLATE_WRAPPER_CLASS = "dx-template-wrapper";
var VALIDATION_TARGET_CLASS = "dx-validation-target";
var INVALID_CLASS = "dx-invalid";
export function renderFieldItem(_ref) {
    var {
        $parent: $parent,
        rootElementCssClassList: rootElementCssClassList,
        formOrLayoutManager: formOrLayoutManager,
        createComponentCallback: createComponentCallback,
        labelOptions: labelOptions,
        labelNeedBaselineAlign: labelNeedBaselineAlign,
        labelLocation: labelLocation,
        needRenderLabel: needRenderLabel,
        formLabelLocation: formLabelLocation,
        item: item,
        editorOptions: editorOptions,
        isSimpleItem: isSimpleItem,
        isRequired: isRequired,
        template: template,
        helpID: helpID,
        labelID: labelID,
        name: name,
        helpText: helpText,
        requiredMessageTemplate: requiredMessageTemplate,
        validationGroup: validationGroup
    } = _ref;
    var $rootElement = $("<div>").addClass(rootElementCssClassList.join(" ")).appendTo($parent);
    $rootElement.addClass(isRequired ? FIELD_ITEM_REQUIRED_CLASS : FIELD_ITEM_OPTIONAL_CLASS);
    if (isSimpleItem) {
        $rootElement.addClass(FLEX_LAYOUT_CLASS)
    }
    if (isSimpleItem && labelNeedBaselineAlign) {
        $rootElement.addClass(FIELD_ITEM_LABEL_ALIGN_CLASS)
    }
    var $fieldEditorContainer = $("<div>");
    $fieldEditorContainer.data("dx-form-item", item);
    $fieldEditorContainer.addClass(FIELD_ITEM_CONTENT_CLASS).addClass(FIELD_ITEM_CONTENT_LOCATION_CLASS + {
        right: "left",
        left: "right",
        top: "bottom"
    } [formLabelLocation]);
    var $label = null;
    if (needRenderLabel) {
        if (labelOptions.labelTemplate) {
            labelOptions.labelTemplateData = getTemplateData(item, editorOptions, formOrLayoutManager)
        }
        $label = renderLabel(labelOptions)
    }
    if ($label) {
        var {
            editorType: editorType
        } = item;
        $rootElement.append($label);
        if ("top" === labelLocation || "left" === labelLocation) {
            $rootElement.append($fieldEditorContainer)
        }
        if ("right" === labelLocation) {
            $rootElement.prepend($fieldEditorContainer)
        }
        if ("top" === labelLocation) {
            $rootElement.addClass(LABEL_VERTICAL_ALIGNMENT_CLASS)
        } else {
            $rootElement.addClass(LABEL_HORIZONTAL_ALIGNMENT_CLASS)
        }
        if ("dxCheckBox" === editorType || "dxSwitch" === editorType) {
            eventsEngine.on($label, clickEventName, (function() {
                eventsEngine.trigger($fieldEditorContainer.children(), clickEventName)
            }))
        }
        var isToggleControls = ["dxCheckBox", "dxSwitch", "dxRadioGroup"].includes(editorType);
        var labelAlignment = labelOptions.alignment;
        var isLabelAlignmentLeft = "left" === labelAlignment || !labelAlignment;
        var hasNotTemplate = !template;
        var isLabelOnTop = "top" === labelLocation;
        if (hasNotTemplate && isToggleControls && isLabelOnTop && isLabelAlignmentLeft) {
            $fieldEditorContainer.addClass(TOGGLE_CONTROLS_PADDING_CLASS)
        }
    } else {
        $rootElement.append($fieldEditorContainer)
    }
    var widgetInstance;
    if (template) {
        template.render({
            container: getPublicElement($fieldEditorContainer),
            model: getTemplateData(item, editorOptions, formOrLayoutManager),
            onRendered() {
                var $validationTarget = getValidationTarget($fieldEditorContainer);
                var validationTargetInstance = tryGetValidationTargetInstance($validationTarget);
                subscribeWrapperInvalidClassToggle(validationTargetInstance)
            }
        })
    } else {
        var $div = $("<div>").appendTo($fieldEditorContainer);
        try {
            widgetInstance = createComponentCallback($div, item.editorType, editorOptions);
            widgetInstance.setAria("describedby", helpID);
            if (labelID) {
                widgetInstance.setAria("labelledby", labelID)
            }
            widgetInstance.setAria("required", isRequired)
        } catch (e) {
            errors.log("E1035", e.message)
        }
    }
    var $validationTarget = getValidationTarget($fieldEditorContainer);
    var validationTargetInstance = $validationTarget && $validationTarget.data(VALIDATION_TARGET_CLASS);
    if (validationTargetInstance) {
        var isItemHaveCustomLabel = item.label && item.label.text;
        var itemName = isItemHaveCustomLabel ? null : name;
        var fieldName = isItemHaveCustomLabel ? item.label.text : itemName && captionize(itemName);
        var validationRules;
        if (isSimpleItem) {
            if (item.validationRules) {
                validationRules = item.validationRules
            } else {
                var requiredMessage = format(requiredMessageTemplate, fieldName || "");
                validationRules = item.isRequired ? [{
                    type: "required",
                    message: requiredMessage
                }] : null
            }
        }
        if (Array.isArray(validationRules) && validationRules.length) {
            createComponentCallback($validationTarget, Validator, {
                validationRules: validationRules,
                validationGroup: validationGroup,
                dataGetter: function() {
                    return {
                        formItem: item
                    }
                }
            })
        }
        subscribeWrapperInvalidClassToggle(validationTargetInstance)
    }
    if (helpText && isSimpleItem) {
        var $editorParent = $fieldEditorContainer.parent();
        $editorParent.append($("<div>").addClass(FIELD_ITEM_CONTENT_WRAPPER_CLASS).append($fieldEditorContainer).append($("<div>").addClass(FIELD_ITEM_HELP_TEXT_CLASS).attr("id", helpID).text(helpText)))
    }
    return {
        $fieldEditorContainer: $fieldEditorContainer,
        $rootElement: $rootElement,
        widgetInstance: widgetInstance
    }
}

function getValidationTarget($fieldEditorContainer) {
    var $editor = $fieldEditorContainer.children().first();
    return $editor.hasClass(TEMPLATE_WRAPPER_CLASS) ? $editor.children().first() : $editor
}

function tryGetValidationTargetInstance($validationTarget) {
    var _$validationTarget$pa, _$validationTarget$pa2;
    return (null === $validationTarget || void 0 === $validationTarget ? void 0 : $validationTarget.data(VALIDATION_TARGET_CLASS)) || (null === $validationTarget || void 0 === $validationTarget ? void 0 : null === (_$validationTarget$pa = $validationTarget.parent) || void 0 === _$validationTarget$pa ? void 0 : null === (_$validationTarget$pa2 = _$validationTarget$pa.call($validationTarget)) || void 0 === _$validationTarget$pa2 ? void 0 : _$validationTarget$pa2.data(VALIDATION_TARGET_CLASS))
}

function subscribeWrapperInvalidClassToggle(validationTargetInstance) {
    if (validationTargetInstance && isMaterial()) {
        var wrapperClass = ".".concat(FIELD_ITEM_CONTENT_WRAPPER_CLASS);
        var toggleInvalidClass = _ref2 => {
            var {
                element: element,
                component: component
            } = _ref2;
            var {
                isValid: isValid,
                validationMessageMode: validationMessageMode
            } = component.option();
            $(element).parents(wrapperClass).toggleClass(INVALID_CLASS, false === isValid && (component._isFocused() || "always" === validationMessageMode))
        };
        validationTargetInstance.on("optionChanged", e => {
            if ("isValid" !== e.name) {
                return
            }
            toggleInvalidClass(e)
        });
        validationTargetInstance.on("focusIn", toggleInvalidClass).on("focusOut", toggleInvalidClass).on("enterKey", toggleInvalidClass)
    }
}

function getTemplateData(item, editorOptions, formOrLayoutManager) {
    return {
        dataField: item.dataField,
        editorType: item.editorType,
        editorOptions: editorOptions,
        component: formOrLayoutManager,
        name: item.name
    }
}
