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());
    });
};
import React, { useState } from 'react';
import { ActionList, ActionListGroup, ActionListItem, Alert, Button, Checkbox, Form, FormGroup, TextArea, Content, TextInput, ContentVariants, ValidatedOptions } from '@patternfly/react-core';
import { LocaleContext } from '../context/LocaleContext';
export const FeedbackForm = ({ email, onCloseModal, onSubmit, onClickBack, handleFeedbackError, modalTitle, modalDescription, textareaLabel, feedbackType, checkboxDescription, textAreaHidden = false, submitTitle }) => {
    const intl = React.useContext(LocaleContext);
    const [currentEmail, setCurrentEmail] = useState(email ? email : '');
    const [textAreaValue, setTextAreaValue] = useState('');
    const [emailValid, setEmailValid] = useState(ValidatedOptions.default);
    const [checked, setChecked] = 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.createElement("div", { className: "chr-c-feedback-content" },
        React.createElement("div", { className: "chr-c-feedback-heading" },
            React.createElement(Content, { component: ContentVariants.h1 }, modalTitle),
            modalDescription),
        React.createElement(Form, { className: "chr-c-feedback-content-main" },
            textAreaHidden ? ('') : (React.createElement(FormGroup, { label: textareaLabel, fieldId: "horizontal-form-exp" },
                React.createElement(TextArea, { value: textAreaValue, onChange: (_event, value) => setTextAreaValue(value), className: "chr-c-feedback-text-area", name: "feedback-description-text", id: "feedback-description-text" }))),
            React.createElement(FormGroup, { className: "pf-u-mt-20" },
                React.createElement(Checkbox, { id: "feedback-checkbox", isChecked: checked, onChange: () => setChecked(!checked), label: intl.researchOpportunities, description: checkboxDescription }))),
        checked ? (React.createElement(React.Fragment, null,
            React.createElement("div", { className: "pf-u-font-family-heading-sans-serif chr-c-feedback-email" }, intl.email),
            React.createElement(TextInput, { value: currentEmail, onChange: (_event, value) => setCurrentEmail(value), validated: emailValid, onBlur: () => !validateEmail(currentEmail)
                    ? setEmailValid(ValidatedOptions.error)
                    : setEmailValid(ValidatedOptions.default), id: "textInput-basic-2", type: "email", "aria-label": "Error state username example" }),
            emailValid === ValidatedOptions.error ? (React.createElement(Alert, { variant: "danger", isInline: true, isPlain: true, title: "Email address is invalid." })) : (React.createElement(React.Fragment, null)))) : (''),
        React.createElement(ActionList, { className: "chr-c-feedback-footer" },
            React.createElement(ActionListGroup, null,
                React.createElement(ActionListItem, null,
                    React.createElement(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.createElement(ActionListItem, null,
                    React.createElement(Button, { ouiaId: "back-feedback", className: "chr-c-feedback-footer-button", key: "back", variant: "secondary", onClick: onClickBack }, intl.back))),
            React.createElement(ActionListGroup, null,
                React.createElement(ActionListItem, null,
                    React.createElement(Button, { ouiaId: "cancel-feedback", className: "chr-c-feedback-footer-button", key: "cancel", variant: "link", onClick: onCloseModal }, intl.cancel))))));
};
