<commit_before>/*
 * Copyright (c) 2021, 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, useEffect, useState, useCallback,
} from 'react';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import { useAPI } from 'AppComponents/Apis/Details/components/ApiContext';
import cloneDeep from 'lodash.clonedeep';
import isEmpty from 'lodash/isEmpty';
import Alert from 'AppComponents/Shared/Alert';
import Banner from 'AppComponents/Shared/Banner';
import API from 'AppData/api';
import CircularProgress from '@material-ui/core/CircularProgress';
import PropTypes from 'prop-types';
import { isRestricted } from 'AppData/AuthManager';
import AsyncOperation from '../Resources/components/AsyncOperation';
import GroupOfOperations from '../Resources/components/operationComponents/asyncapi/GroupOfOperations';
import AddOperation from '../Resources/components/AddOperation';
import SubscriptionConfig from '../Resources/components/operationComponents/asyncapi/SubscriptionConfig';
import { extractAsyncAPIPathParameters } from '../Resources/operationUtils';
import SaveOperations from '../Resources/components/SaveOperations';

const verbMap = {
    sub: 'subscribe',
    pub: 'publish',
};

/**
 * This component handles the Resource page in API details though it's written in a sharable way
 * that anyone could use this to render resources in anywhere else if needed.
 *
 * @export
 * @returns {React.Component} @inheritdoc
 */
export default function Topics(props) {
    const {
        disableUpdate,
        disableAddOperation,
    } = props;

    const [api, updateAPI] = useAPI();
    const [pageError, setPageError] = useState(false);
    const [sharedScopes, setSharedScopes] = useState();
    const [sharedScopesByName, setSharedScopesByName] = useState();
    const [asyncAPISpec, setAsyncAPISpec] = useState({});
    const [securityDefScopes, setSecurityDefScopes] = useState({});
    const isAsyncAPI = api.type === 'WEBSUB' || api.type === 'WS' || api.type === 'SSE';
    const [markedOperations, setSelectedOperation] = useState({});

    /**
     *
     * @param {*} spec
     * @param {*} ref
     */
    function getRefTarget(spec, ref) {
        const arr = ref.split('/');
        const i = (arr[0] === '#') ? 1 : 0;
        let target = spec;
        for (let j = i; j < arr.length; j++) {
            target = target[arr[j]];
        }
        return target;
    }

    /**
     *
     * @param {*} spec
     * @param {*} parent
     */
    function resolveSpec(spec, source) {
        if (typeof source === 'object') {
            let o = {};
            Object.entries(source).forEach(([k, v]) => {
                if (v !== null) {
                    if (k !== '$ref') {
                        o[k] = resolveSpec(spec, v);
                    } else {
                        const resolvedRef = resolveSpec(spec, getRefTarget(spec, v));
                        o = { ...o, ...resolvedRef };
                    }
                }
            });
            return o;
        }
        return source;
    }

    /**
     *
     * @param {*} state
     * @param {*} configAction
     */
    function websubSubscriptionConfigReducer(state, configAction) {
        const { action, value } = configAction;
        const nextState = { ...state };
        switch (action) {
            case 'signingAlgorithm':
            case 'signatureHeader':
            case 'secret':
                nextState[action] = value;
                break;
            default:
                return nextState;
        }
        return nextState;
    }
    const initialWebsubSubscriptionConfig = api.websubSubscriptionConfiguration || {
        signingAlgorithm: '',
        signatureHeader: 'x-hub-signature',
        secret: '',
    };

    const [websubSubscriptionConfiguration, websubSubscriptionConfigDispatcher] = useReducer(
        websubSubscriptionConfigReducer, initialWebsubSubscriptionConfig,
    );

    /**
     *
     *
     * @param {*} currenPaths
     * @param {*} action
     */
    function operationsReducer(currentOperations, operationAction) {
        const { action, data } = operationAction;
        const { target, verb, value } = data || {};
        const addedOperations = cloneDeep(currentOperations);
        let updatedOperation;
        if (target) {
            updatedOperation = cloneDeep(currentOperations[target]);
        }

        switch (action) {
            case 'init':
                return data;
            case 'description':
                updatedOperation[action] = value;
                return {
                    ...currentOperations,
                    [target]: { ...currentOperations[target], description: updatedOperation.description },
                };
            case 'uriMapping':
                return {
                    ...currentOperations,
                    [target]: {
                        ...currentOperations[target],
                        [verb]: {<commit_msg>fix clearing issue in reset btn<commit_after>/*
 * Copyright (c) 2021, 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, useEffect, useState, useCallback,
} from 'react';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import { useAPI } from 'AppComponents/Apis/Details/components/ApiContext';
import cloneDeep from 'lodash.clonedeep';
import isEmpty from 'lodash/isEmpty';
import Alert from 'AppComponents/Shared/Alert';
import Banner from 'AppComponents/Shared/Banner';
import API from 'AppData/api';
import CircularProgress from '@material-ui/core/CircularProgress';
import PropTypes from 'prop-types';
import { isRestricted } from 'AppData/AuthManager';
import AsyncOperation from '../Resources/components/AsyncOperation';
import GroupOfOperations from '../Resources/components/operationComponents/asyncapi/GroupOfOperations';
import AddOperation from '../Resources/components/AddOperation';
import SubscriptionConfig from '../Resources/components/operationComponents/asyncapi/SubscriptionConfig';
import { extractAsyncAPIPathParameters } from '../Resources/operationUtils';
import SaveOperations from '../Resources/components/SaveOperations';

const verbMap = {
    sub: 'subscribe',
    pub: 'publish',
};

/**
 * This component handles the Resource page in API details though it's written in a sharable way
 * that anyone could use this to render resources in anywhere else if needed.
 *
 * @export
 * @returns {React.Component} @inheritdoc
 */
export default function Topics(props) {
    const {
        disableUpdate,
        disableAddOperation,
    } = props;

    const [api, updateAPI] = useAPI();
    const [pageError, setPageError] = useState(false);
    const [sharedScopes, setSharedScopes] = useState();
    const [sharedScopesByName, setSharedScopesByName] = useState();
    const [asyncAPISpec, setAsyncAPISpec] = useState({});
    const [securityDefScopes, setSecurityDefScopes] = useState({});
    const isAsyncAPI = api.type === 'WEBSUB' || api.type === 'WS' || api.type === 'SSE';
    const [markedOperations, setSelectedOperation] = useState({});

    /**
     *
     * @param {*} spec
     * @param {*} ref
     */
    function getRefTarget(spec, ref) {
        const arr = ref.split('/');
        const i = (arr[0] === '#') ? 1 : 0;
        let target = spec;
        for (let j = i; j < arr.length; j++) {
            target = target[arr[j]];
        }
        return target;
    }

    /**
     *
     * @param {*} spec
     * @param {*} parent
     */
    function resolveSpec(spec, source) {
        if (typeof source === 'object') {
            let o = {};
            Object.entries(source).forEach(([k, v]) => {
                if (v !== null) {
                    if (k !== '$ref') {
                        o[k] = resolveSpec(spec, v);
                    } else {
                        const resolvedRef = resolveSpec(spec, getRefTarget(spec, v));
                        o = { ...o, ...resolvedRef };
                    }
                }
            });
            return o;
        }
        return source;
    }

    /**
     *
     * @param {*} state
     * @param {*} configAction
     */
    function websubSubscriptionConfigReducer(state, configAction) {
        const { action, value } = configAction;
        const nextState = { ...state };
        switch (action) {
            case 'signingAlgorithm':
            case 'signatureHeader':
            case 'secret':
                nextState[action] = value;
                break;
            default:
                return nextState;
        }
        return nextState;
    }
    const initialWebsubSubscriptionConfig = api.websubSubscriptionConfiguration || {
        signingAlgorithm: '',
        signatureHeader: 'x-hub-signature',
        secret: '',
    };

    const [websubSubscriptionConfiguration, websubSubscriptionConfigDispatcher] = useReducer(
        websubSubscriptionConfigReducer, initialWebsubSubscriptionConfig,
    );

    /**
     *
     *
     * @param {*} currenPaths
     * @param {*} action
     */
    function operationsReducer(currentOperations, operationAction) {
        const { action, data } = operationAction;
        const { target, verb, value } = data || {};
        const addedOperations = cloneDeep(currentOperations);
        let updatedOperation;
        if (target) {
            updatedOperation = cloneDeep(currentOperations[target]);
        }

        switch (action) {
            case 'init':
                setSelectedOperation({});
                return data || asyncAPISpec.channels;
            case 'description':
                updatedOperation[action] = value;
                return {
                    ...currentOperations,
                    [target]: { ...currentOperations[target], description: updatedOperation.description },
                };
            case 'uriMapping':
                return {
                    ...currentOperations,
                    [target]: {
                        ...currentOperations[target],
                        [verb]: {
{'fraction_non_alphanumeric': 0.11626468769325912, 'fraction_numerical': 0.0024737167594310453, 'mean_word_length': 2.2868757259001162, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2504356', 'n_tokens_mistral': 2947, 'n_tokens_neox': 2925, 'n_words': 827}