"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextFormControl = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const styled_components_1 = tslib_1.__importDefault(require("styled-components"));
const Tooltip_1 = tslib_1.__importDefault(require("@material-ui/core/Tooltip"));
const TextEntryFormControl_1 = require("../TextEntryFormControl");
const FormDesignConstants_1 = require("../../Utilities/FormDesignConstants");
const CommonStyled_1 = require("./CommonStyled");
const StringDataDefinition_1 = require("@appglue/common/dist/Common/Data/Definitions/StringDataDefinition");
const DataUtilities_1 = require("@appglue/common/dist/Common/DataUtilities");
//@RegisterUIControl('Data (Entry)', 'Text Field', ControlType.Control, <TextFieldIcon />)
class TextFormControl extends TextEntryFormControl_1.TextEntryFormControl {
    __type = "Text Field";
    render() {
        let style = this.getFormContext()?.form?.defaultTextStyle;
        if (this.overrideStyle && this.style)
            style = this.style;
        let size = this.getFormContext()?.form?.defaultTextSize ?? "medium";
        if (this.overrideStyle && this.size)
            size = this.size;
        const issueData = this.getFormContext().getControlContext(this).getRuntimeIssueData();
        const issueText = issueData?.text || "";
        const customWidth = this.fullWidth ? "100%" : this.width ? `${this.width}px` : "200px";
        switch (style) {
            case FormDesignConstants_1.TextControlStyle.LABELED:
                return ((0, jsx_runtime_1.jsxs)(StyledTextFieldWrap, { width: customWidth, children: [this.label && ((0, jsx_runtime_1.jsx)(CommonStyled_1.StyledInputLabel, { "data-role": FormDesignConstants_1.TextControlStyle.LABELED, children: this.label })), (0, jsx_runtime_1.jsx)(CommonStyled_1.StyledTextField, { size: size, "data-size": size, "data-testid": this.valueName, variant: "outlined", fullWidth: this.fullWidth, width: customWidth, value: this.valueName ? this.getFormDataValue(this.valueName) || "" : "", onChange: this.handleChange, error: Boolean(issueText), InputProps: {
                                inputRef: this.ref,
                            } }), issueText && issueText.length > 30 && ((0, jsx_runtime_1.jsx)(Tooltip_1.default, { title: issueText, arrow: true, placement: "bottom", children: (0, jsx_runtime_1.jsx)(CommonStyled_1.StyledFormHelperText, { error: Boolean(issueText), "data-testid": `${this.valueName || "textfield"}-hinttext`, children: issueText.slice(0, 30) + "..." }) })), ((issueText && issueText.length < 31) || this.hintText) && ((0, jsx_runtime_1.jsx)(CommonStyled_1.StyledFormHelperText, { error: Boolean(issueText), "data-testid": `${this.valueName || "textfield"}-hinttext`, children: issueText ? issueText : this.hintText }))] }));
            case FormDesignConstants_1.TextControlStyle.SHADED:
                return ((0, jsx_runtime_1.jsxs)(StyledTextFieldWrap, { width: customWidth, children: [(0, jsx_runtime_1.jsx)(CommonStyled_1.StyledTextField, { size: size, "data-size": size, "data-testid": this.valueName, variant: "filled", customstyle: "filled", fullWidth: this.fullWidth, width: customWidth, value: this.valueName ? this.getFormDataValue(this.valueName) || "" : "", onChange: this.handleChange, error: Boolean(issueText), label: this.valueName, InputProps: {
                                inputRef: this.ref,
                            } }), (issueText || this.hintText) && ((0, jsx_runtime_1.jsx)(CommonStyled_1.StyledFormHelperText, { error: Boolean(issueText), "data-testid": `${this.valueName || "textfield"}-hinttext`, children: issueText ? issueText : this.hintText }))] }));
            case FormDesignConstants_1.TextControlStyle.UNDERLINED:
                return ((0, jsx_runtime_1.jsxs)(StyledTextFieldWrap, { width: customWidth, children: [(0, jsx_runtime_1.jsx)(CommonStyled_1.StyledTextField, { size: size, "data-size": size, "data-testid": this.valueName, variant: "standard", customstyle: "standard", fullWidth: this.fullWidth, width: customWidth, value: this.valueName ? this.getFormDataValue(this.valueName) || "" : "", onChange: this.handleChange, error: Boolean(issueText), label: this.valueName, InputProps: {
                                inputRef: this.ref,
                            } }), (issueText || this.hintText) && ((0, jsx_runtime_1.jsx)(CommonStyled_1.StyledFormHelperText, { error: Boolean(issueText), "data-testid": `${this.valueName || "textfield"}-hinttext`, children: issueText ? issueText : this.hintText }))] }));
            case FormDesignConstants_1.TextControlStyle.OUTLINE:
                return ((0, jsx_runtime_1.jsxs)(StyledTextFieldWrap, { width: customWidth, children: [(0, jsx_runtime_1.jsx)(CommonStyled_1.StyledTextField, { size: size, "data-size": size, "data-testid": this.valueName, variant: "outlined", customstyle: "outlined", fullWidth: this.fullWidth, width: customWidth, value: this.valueName ? this.getFormDataValue(this.valueName) || "" : "", onChange: this.handleChange, error: Boolean(issueText), label: this.valueName, InputProps: {
                                inputRef: this.ref,
                            } }), (issueText || this.hintText) && ((0, jsx_runtime_1.jsx)(CommonStyled_1.StyledFormHelperText, { error: Boolean(issueText), "data-testid": `${this.valueName || "textfield"}-hinttext`, children: issueText ? issueText : this.hintText }))] }));
        }
    }
    renderEditUI() {
        return (0, jsx_runtime_1.jsx)(XTextFieldEditUI, { editMe: this });
    }
    getAddedData() {
        if (!this.valueName)
            return;
        let def = new StringDataDefinition_1.StringDataDefinition();
        def.name = this.valueName;
        return def;
    }
    getStorageData() {
        let retData = super.getStorageData();
        retData = DataUtilities_1.DataUtilities.addStorageData(this, retData, "__type");
        return retData;
    }
    setStorageData(data) {
        super.setStorageData(data);
        DataUtilities_1.DataUtilities.setStorageData(this, data, "__type");
    }
    handleChange = (event) => {
        if (this.valueName) {
            this.setFormDataValue(this.valueName, event.currentTarget.value);
        }
    };
}
exports.TextFormControl = TextFormControl;
function XTextFieldEditUI(props) {
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [props.editMe.renderBaseDataControlEditor(), props.editMe.renderTextControlBasePropertyEditor(), props.editMe.renderTextStyleSelectionEditor()] }));
}
const StyledTextFieldWrap = styled_components_1.default.div `
    width: ${({ width }) => width} !important;
    height: 100% !important;
    box-sizing: border-box !important;
    position: relative !important;
`;
//# sourceMappingURL=TextFormControl.js.map