<commit_before>/**
 *  Copyright (C) 2021 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import { get } from 'lodash';
import * as Yup from 'yup';

import { Tooltip } from '@material-ui/core';
import Button from '@material-ui/core/Button';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import InfoIcon from '@material-ui/icons/InfoOutlined';
import { connect, withFormik, Field, Form } from 'formik';

import {
	CRITERIA_LIST,
	CRITERIA_OPERATORS_LABELS,
	CRITERIA_OPERATORS_TYPES,
	REGEX_INFO_TEXT,
	REGEX_INFO_URL,
	VALUE_DATA_TYPES,
	VALUE_FIELD_MAP
} from '../../../constants/criteria';
import { renderWhenTrue } from '../../../helpers/rendering';
import { schema, VALIDATIONS_MESSAGES } from '../../../services/validation';
import { AutosuggestField } from '../autosuggestField/autosuggestField.component';
import { CriteriaValueField } from './components/criteriaValueField/criteriaValueField.component';
import { RegexInfoLink } from './components/criteriaValueField/criteriaValueField.styles';
import {
	CustomError,
	FormControl,
	NewCriterionFooter,
	OperatorSubheader,
	SelectField,
	SelectFieldValue
} from './criteriaField.styles';

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const PaperPropsStyle = {
	maxHeight: ITEM_HEIGHT * 8.5 + ITEM_PADDING_TOP,
	width: 250,
	transform: 'translate3d(0, 0, 0)'
};

const CriterionSchema = Yup.object().shape({
	field: Yup.string().required(),
	operator: Yup.string().required(),
	values: Yup.array().when('operator', (operator, arraySchema) => {
		const dataType = get(VALUE_FIELD_MAP[operator], 'dataType');

		if (!dataType) {
			return arraySchema;
		}

		if (dataType === VALUE_DATA_TYPES.NUMBER) {
			return Yup.array().of(schema.measureNumberDecimal.nullable()).required(VALIDATIONS_MESSAGES.REQUIRED);
		}

		return arraySchema.of(Yup.string().required(VALIDATIONS_MESSAGES.REQUIRED));
	})
});

interface IProps {
	values: any;
	criterion: any;
	fieldNames: any[];
	formik: any;
	alreadySelectedFilters: any[];
	selectedCriterion: string;
	setState: (criterionForm) => void;
	onSubmit: (values) => void;
	handleSubmit: () => void;
}

interface IState {
	filterAlreadyExistsError: boolean;
}

class NewCreaterionFormComponent extends React.PureComponent<IProps, IState> {
	public state = {
		filterAlreadyExistsError: false
	};

	public renderRegexInfo = renderWhenTrue(() => (
		<RegexInfoLink href={REGEX_INFO_URL} target="_blank"><commit_msg>ISSUE #3057 -  remove react imports<commit_after>/**
 *  Copyright (C) 2021 3D Repo Ltd
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import { PureComponent } from 'react';
import { get } from 'lodash';
import * as Yup from 'yup';

import { Tooltip } from '@material-ui/core';
import Button from '@material-ui/core/Button';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import InfoIcon from '@material-ui/icons/InfoOutlined';
import { connect, withFormik, Field, Form } from 'formik';

import {
	CRITERIA_LIST,
	CRITERIA_OPERATORS_LABELS,
	CRITERIA_OPERATORS_TYPES,
	REGEX_INFO_TEXT,
	REGEX_INFO_URL,
	VALUE_DATA_TYPES,
	VALUE_FIELD_MAP
} from '../../../constants/criteria';
import { renderWhenTrue } from '../../../helpers/rendering';
import { schema, VALIDATIONS_MESSAGES } from '../../../services/validation';
import { AutosuggestField } from '../autosuggestField/autosuggestField.component';
import { CriteriaValueField } from './components/criteriaValueField/criteriaValueField.component';
import { RegexInfoLink } from './components/criteriaValueField/criteriaValueField.styles';
import {
	CustomError,
	FormControl,
	NewCriterionFooter,
	OperatorSubheader,
	SelectField,
	SelectFieldValue
} from './criteriaField.styles';

const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const PaperPropsStyle = {
	maxHeight: ITEM_HEIGHT * 8.5 + ITEM_PADDING_TOP,
	width: 250,
	transform: 'translate3d(0, 0, 0)'
};

const CriterionSchema = Yup.object().shape({
	field: Yup.string().required(),
	operator: Yup.string().required(),
	values: Yup.array().when('operator', (operator, arraySchema) => {
		const dataType = get(VALUE_FIELD_MAP[operator], 'dataType');

		if (!dataType) {
			return arraySchema;
		}

		if (dataType === VALUE_DATA_TYPES.NUMBER) {
			return Yup.array().of(schema.measureNumberDecimal.nullable()).required(VALIDATIONS_MESSAGES.REQUIRED);
		}

		return arraySchema.of(Yup.string().required(VALIDATIONS_MESSAGES.REQUIRED));
	})
});

interface IProps {
	values: any;
	criterion: any;
	fieldNames: any[];
	formik: any;
	alreadySelectedFilters: any[];
	selectedCriterion: string;
	setState: (criterionForm) => void;
	onSubmit: (values) => void;
	handleSubmit: () => void;
}

interface IState {
	filterAlreadyExistsError: boolean;
}

class NewCreaterionFormComponent extends PureComponent<IProps, IState> {
	public state = {
		filterAlreadyExistsError: false
	};

	public renderRegexInfo = renderWhenTrue(() => (
		<RegexInfoLink href={REGEX_INFO_URL} target="_blank">
{'fraction_non_alphanumeric': 0.1197205462051445, 'fraction_numerical': 0.006351222610352493, 'mean_word_length': 4.875932835820896, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '261774', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1966, 'n_words': 547}