import React, { useCallback, useState } from 'react';
import { useSelector } from 'react-redux';
import {
Button,
Hint,
HintBody,
DropdownItem,
DropdownSeparator,
Dropdown,
Skeleton,
Split,
SplitItem,
ActionList,
ActionListItem,
KebabToggle,
} from '@patternfly/react-core';
import { TableVariant, Thead, Tbody, Tr, Th, Td } from '@patternfly/react-table';
import { translate as __ } from 'foremanReact/common/I18n';
import { selectAPIResponse } from 'foremanReact/redux/API/APISelectors';
import { urlBuilder } from 'foremanReact/common/urlHelpers';
import SelectableDropdown from '../../../SelectableDropdown';
import TableWrapper from '../../../../components/Table/TableWrapper';
import { useUrlParams } from '../../../../components/Table/TableHooks';
import { PackagesStatus, PackagesLatestVersion } from '../../../../components/Packages';
import { getInstalledPackagesWithLatest } from '../HostPackages/HostPackagesActions';
import { selectHostPackagesStatus } from '../HostPackages/HostPackagesSelectors';
import { HOST_PACKAGES_KEY, PACKAGES_VERSION_STATUSES, VERSION_STATUSES_TO_PARAM } from '../HostPackages/HostPackagesConstants';
import './PackagesTab.scss';
import hostIdNotReady from '../HostDetailsActions';
import PackageInstallModal from './PackageInstallModal';
import defaultRemoteActionMethod, { KATELLO_AGENT } from '../hostDetailsHelpers';
export const PackagesTab = () => {
const hostDetails = useSelector(state => selectAPIResponse(state, 'HOST_DETAILS'));
const { id: hostId, name: hostName } = hostDetails;
const { searchParam } = useUrlParams();
const [searchQuery, updateSearchQuery] = useState(searchParam || '');
const PACKAGE_STATUS = __('Status');
const [packageStatusSelected, setPackageStatusSelected] = useState(PACKAGE_STATUS);
const activeFilters = [packageStatusSelected];
const defaultFilters = [PACKAGE_STATUS];
const [isBulkActionOpen, setIsBulkActionOpen] = useState(false);
const toggleBulkAction = () => setIsBulkActionOpen(prev => !prev);
const [isModalOpen, setIsModalOpen] = useState(false);
const closeModal = () => setIsModalOpen(false);
const showKatelloAgent = (defaultRemoteActionMethod({ hostDetails }) === KATELLO_AGENT);
const emptyContentTitle = __('This host does not have any packages.');
const emptyContentBody = __('Packages will appear here when available.');
const emptySearchTitle = __('No matching packages found');
const emptySearchBody = __('Try changing your search settings.');
const columnHeaders = [
__('Package'),
__('Status'),
__('Installed Version'),
__('Upgradable To'),
];
const fetchItems = useCallback(
(params) => {
if (!hostId) return hostIdNotReady;
const modifiedParams = { ...params };
if (packageStatusSelected !== PACKAGE_STATUS) {
modifiedParams.status = VERSION_STATUSES_TO_PARAM[packageStatusSelected];
}
return getInstalledPackagesWithLatest(hostId, modifiedParams);
},
[hostId, PACKAGE_STATUS, packageStatusSelected],
);
const response = useSelector(state => selectAPIResponse(state, HOST_PACKAGES_KEY));
const { results, ...metadata } = response;
const status = useSelector(state => selectHostPackagesStatus(state));
if (!hostId) return