<commit_before>/*
 * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import React, { useReducer, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import TextField from '@material-ui/core/TextField';
import { useIntl, FormattedMessage } from 'react-intl';
import { Link as RouterLink } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Alert from 'AppComponents/Shared/Alert';
import ContentBase from 'AppComponents/AdminPages/Addons/ContentBase';
import Box from '@material-ui/core/Box';
import Typography from '@material-ui/core/Typography';
import Grid from '@material-ui/core/Grid';
import ConditionalGroup from 'AppComponents/Throttling/Advanced/ConditionalGroup';
import cloneDeep from 'lodash.clonedeep';
import HelpLinks from 'AppComponents/Throttling/Advanced/HelpLinks';
import API from 'AppData/api';
import AddEditExecution from 'AppComponents/Throttling/Advanced/AddEditExecution';
import CircularProgress from '@material-ui/core/CircularProgress';

const useStyles = makeStyles((theme) => ({
    error: {
        color: theme.palette.error.dark,
    },
    hr: {
        border: 'solid 1px #efefef',
    },
    root: {
        marginBottom: theme.spacing(15),
    },
}));


/**
 * Reducer
 * @param {JSON} state The second number.
 * @returns {Promise}.
 */
function reducer(state, { field, value }) {
    const nextState = cloneDeep(state);
    switch (field) {
        case 'all': // We set initial state with this.
            return value;
        case 'policyName':
        case 'description':
        case 'conditionalGroups':
            nextState[field] = value;
            return nextState;
        case 'defaultLimit':
            if (value === 'REQUESTCOUNTLIMIT') {
                const { defaultLimit: { bandwidth: { timeUnit, unitTime } } } = nextState;
                nextState.defaultLimit.requestCount = {
                    timeUnit, unitTime, requestCount: 0,
                };
                nextState.defaultLimit.type = 'REQUESTCOUNTLIMIT';
                nextState.defaultLimit.bandwidth = null;
            } else {
                const { defaultLimit: { requestCount: { timeUnit, unitTime } } } = nextState;
                nextState.defaultLimit.bandwidth = {
                    timeUnit, unitTime, dataAmount: 0, dataUnit: 'KB',
                };
                nextState.defaultLimit.bandwidth.type = 'BANDWIDTHLIMIT';
                nextState.defaultLimit.requestCount = null;
            }
            nextState.defaultLimit[field] = value;
            return nextState;
        case 'dataUnit':
        case 'requestCount':
        case 'timeUnit':
        case 'dataAmount':
        case 'unitTime':
            if (nextState.defaultLimit.requestCount) {
                nextState.defaultLimit.requestCount[field] = value;
            } else {
                nextState.defaultLimit.bandwidth[field] = value;
            }
            return nextState;
        default:
            return nextState;
    }
}

/**
 * Render a list
 * @returns {JSX} Header AppBar components.<commit_msg>Fix bandwidth policy creation issue<commit_after>/*
 * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import React, { useReducer, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import TextField from '@material-ui/core/TextField';
import { useIntl, FormattedMessage } from 'react-intl';
import { Link as RouterLink } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Alert from 'AppComponents/Shared/Alert';
import ContentBase from 'AppComponents/AdminPages/Addons/ContentBase';
import Box from '@material-ui/core/Box';
import Typography from '@material-ui/core/Typography';
import Grid from '@material-ui/core/Grid';
import ConditionalGroup from 'AppComponents/Throttling/Advanced/ConditionalGroup';
import cloneDeep from 'lodash.clonedeep';
import HelpLinks from 'AppComponents/Throttling/Advanced/HelpLinks';
import API from 'AppData/api';
import AddEditExecution from 'AppComponents/Throttling/Advanced/AddEditExecution';
import CircularProgress from '@material-ui/core/CircularProgress';

const useStyles = makeStyles((theme) => ({
    error: {
        color: theme.palette.error.dark,
    },
    hr: {
        border: 'solid 1px #efefef',
    },
    root: {
        marginBottom: theme.spacing(15),
    },
}));


/**
 * Reducer
 * @param {JSON} state The second number.
 * @returns {Promise}.
 */
function reducer(state, { field, value }) {
    const nextState = cloneDeep(state);
    switch (field) {
        case 'all': // We set initial state with this.
            return value;
        case 'policyName':
        case 'description':
        case 'conditionalGroups':
            nextState[field] = value;
            return nextState;
        case 'defaultLimit':
            if (value === 'REQUESTCOUNTLIMIT') {
                const { defaultLimit: { bandwidth: { timeUnit, unitTime } } } = nextState;
                nextState.defaultLimit.requestCount = {
                    timeUnit, unitTime, requestCount: 0,
                };
                nextState.defaultLimit.type = 'REQUESTCOUNTLIMIT';
                nextState.defaultLimit.bandwidth = null;
            } else {
                const { defaultLimit: { requestCount: { timeUnit, unitTime } } } = nextState;
                nextState.defaultLimit.bandwidth = {
                    timeUnit, unitTime, dataAmount: 0, dataUnit: 'KB',
                };
                nextState.defaultLimit.type = 'BANDWIDTHLIMIT';
                nextState.defaultLimit.requestCount = null;
            }
            return nextState;
        case 'dataUnit':
        case 'requestCount':
        case 'timeUnit':
        case 'dataAmount':
        case 'unitTime':
            if (nextState.defaultLimit.requestCount) {
                nextState.defaultLimit.requestCount[field] = value;
            } else {
                nextState.defaultLimit.bandwidth[field] = value;
            }
            return nextState;
        default:
            return nextState;
    }
}

/**
 * Render a list
 * @returns {JSX} Header AppBar components.
{'fraction_non_alphanumeric': 0.10569865207527025, 'fraction_numerical': 0.004270652609101829, 'mean_word_length': 2.623791102514507, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1572483', 'n_tokens_mistral': 2049, 'n_tokens_neox': 2001, 'n_words': 593}