"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeedbackForm = void 0;
const react_1 = __importStar(require("react"));
const react_core_1 = require("@patternfly/react-core");
const LocaleContext_1 = require("../context/LocaleContext");
const FeedbackForm = ({ email, onCloseModal, onSubmit, onClickBack, handleFeedbackError, modalTitle, modalDescription, textareaLabel, feedbackType, checkboxDescription, textAreaHidden = false, submitTitle }) => {
    const intl = react_1.default.useContext(LocaleContext_1.LocaleContext);
    const [currentEmail, setCurrentEmail] = (0, react_1.useState)(email ? email : '');
    const [textAreaValue, setTextAreaValue] = (0, react_1.useState)('');
    const [emailValid, setEmailValid] = (0, react_1.useState)(react_core_1.ValidatedOptions.default);
    const [checked, setChecked] = (0, react_1.useState)(false);
    function handleModalSubmission() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const emailAddress = checked && currentEmail ? currentEmail : '';
                onSubmit(emailAddress, textAreaValue, checked);
            }
            catch (err) {
                handleFeedbackError();
            }
        });
    }
    const validateEmail = (email) => email
        .toLowerCase()
        .match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
    const isSubmitButtonDisabled = () => {
        if (feedbackType !== '[Research Opportunities]') {
            if (textAreaValue.length > 1 && checked && validateEmail(currentEmail)) {
                return false;
            }
            else if (textAreaValue.length > 1 && !checked) {
                return false;
            }
            return true;
        }
        else {
            if (checked && validateEmail(currentEmail)) {
                return false;
            }
            else {
                return true;
            }
        }
    };
    return (react_1.default.createElement("div", { className: "chr-c-feedback-content" },
        react_1.default.createElement("div", { className: "chr-c-feedback-heading" },
            react_1.default.createElement(react_core_1.Content, { component: react_core_1.ContentVariants.h1 }, modalTitle),
            modalDescription),
        react_1.default.createElement(react_core_1.Form, { className: "chr-c-feedback-content-main" },
            textAreaHidden ? ('') : (react_1.default.createElement(react_core_1.FormGroup, { label: textareaLabel, fieldId: "horizontal-form-exp" },
                react_1.default.createElement(react_core_1.TextArea, { value: textAreaValue, onChange: (_event, value) => setTextAreaValue(value), className: "chr-c-feedback-text-area", name: "feedback-description-text", id: "feedback-description-text" }))),
            react_1.default.createElement(react_core_1.FormGroup, { className: "pf-u-mt-20" },
                react_1.default.createElement(react_core_1.Checkbox, { id: "feedback-checkbox", isChecked: checked, onChange: () => setChecked(!checked), label: intl.researchOpportunities, description: checkboxDescription }))),
        checked ? (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement("div", { className: "pf-u-font-family-heading-sans-serif chr-c-feedback-email" }, intl.email),
            react_1.default.createElement(react_core_1.TextInput, { value: currentEmail, onChange: (_event, value) => setCurrentEmail(value), validated: emailValid, onBlur: () => !validateEmail(currentEmail)
                    ? setEmailValid(react_core_1.ValidatedOptions.error)
                    : setEmailValid(react_core_1.ValidatedOptions.default), id: "textInput-basic-2", type: "email", "aria-label": "Error state username example" }),
            emailValid === react_core_1.ValidatedOptions.error ? (react_1.default.createElement(react_core_1.Alert, { variant: "danger", isInline: true, isPlain: true, title: "Email address is invalid." })) : (react_1.default.createElement(react_1.default.Fragment, null)))) : (''),
        react_1.default.createElement(react_core_1.ActionList, { className: "chr-c-feedback-footer" },
            react_1.default.createElement(react_core_1.ActionListGroup, null,
                react_1.default.createElement(react_core_1.ActionListItem, null,
                    react_1.default.createElement(react_core_1.Button, { ouiaId: "submit-feedback", className: "chr-c-feedback-footer-button", key: "confirm", variant: "primary", 
                        // eslint-disable-next-line no-nested-ternary
                        isDisabled: isSubmitButtonDisabled(), onClick: handleModalSubmission }, submitTitle)),
                react_1.default.createElement(react_core_1.ActionListItem, null,
                    react_1.default.createElement(react_core_1.Button, { ouiaId: "back-feedback", className: "chr-c-feedback-footer-button", key: "back", variant: "secondary", onClick: onClickBack }, intl.back))),
            react_1.default.createElement(react_core_1.ActionListGroup, null,
                react_1.default.createElement(react_core_1.ActionListItem, null,
                    react_1.default.createElement(react_core_1.Button, { ouiaId: "cancel-feedback", className: "chr-c-feedback-footer-button", key: "cancel", variant: "link", onClick: onCloseModal }, intl.cancel))))));
};
exports.FeedbackForm = FeedbackForm;
