import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { capitalize, head } from 'lodash';
import { Split, SplitItem, GridItem, TextContent, Text, TextVariants, Label } from '@patternfly/react-core';
import { translate as __ } from 'foremanReact/common/I18n';
import { useDispatch } from 'react-redux';
import { getCVFilterDetails, editCVFilter } from '../ContentViewDetailActions';
import AffectedRepositorySelection from './AffectedRepositories/AffectedRepositorySelection';
import RepoIcon from '../Repositories/RepoIcon';
import { repoType } from '../../../../utils/helpers';
import EditableTextInput from '../../../../components/EditableTextInput';
import { hasPermission } from '../../helpers';
const ContentViewFilterDetailsHeader = ({
cvId, filterId, filterDetails, setShowAffectedRepos, details,
}) => {
const dispatch = useDispatch();
const [currentAttribute, setCurrentAttribute] = useState('');
const {
type, name, inclusion, description, rules,
} = filterDetails;
const { permissions } = details;
const errataByDate = !!(type === 'erratum' && head(rules)?.types);
const repositoryType = repoType(type);
const displayedType = () => {
if (errataByDate) return __('Errata - by date range');
if (type) return capitalize(type.replace(/_/g, ' '));
return '';
};
const onEdit = (val, attribute) => {
if (val === filterDetails[attribute]) return;
dispatch(editCVFilter(
filterId,
{ [attribute]: val },
() => dispatch(getCVFilterDetails(cvId, filterId)),
));
};
return (
<>
{displayedType()}
>
);
};
ContentViewFilterDetailsHeader.propTypes = {
cvId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
filterId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
filterDetails: PropTypes.shape({
name: PropTypes.string,
type: PropTypes.string,
inclusion: PropTypes.bool,
description: PropTypes.string,
rules: PropTypes.arrayOf(PropTypes.shape({ types: PropTypes.arrayOf(PropTypes.string) })),
}).isRequired,
setShowAffectedRepos: PropTypes.func.isRequired,
details: PropTypes.shape({
permissions: PropTypes.shape({}),
}).isRequired,
};
ContentViewFilterDetailsHeader.defaultProps = {
cvId: '',
filterId: '',
};
export default ContentViewFilterDetailsHeader;