import _debounce from "lodash/fp/debounce";
import _isEmpty from "lodash/fp/isEmpty";
import _isNil from "lodash/fp/isNil";
import _size from "lodash/fp/size";
import _map from "lodash/fp/map";
import _filter from "lodash/fp/filter";
import _get from "lodash/fp/get";
import React, { useState, useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
import Provider from '../../atom/provider';
import { SelectOptionPropTypes } from '../../atom/select';
import ButtonLink from '../../atom/button-link';
import SearchForm from '../../molecule/search-form';
import CardsGrid from '../../organism/cards-grid';
import InputSwitch from '../../atom/input-switch';
import Banner from '../../molecule/banner';
import { COLORS } from '../../variables/colors';
import style from './all-courses.css';

// @ts-expect-error convert untypped
const uncappedMap = _map.convert({
  cap: false
});
const FilterButton = (props, context) => {
  const {
    dataName,
    ariaLabel,
    selected,
    label,
    onClick
  } = props;
  const {
    skin
  } = context;
  const primarySkinColor = _get('common.primary', skin);
  const buttonProps = {
    customStyle: {
      backgroundColor: selected ? primarySkinColor : COLORS.cm_grey_150,
      color: selected ? COLORS.white : COLORS.cm_grey_500,
      transition: 'background-color 0.15s ease-in-out, color 0.15s ease-in-out',
      width: 'fit-content'
    },
    label,
    onClick,
    'data-name': dataName,
    'aria-label': ariaLabel
  };
  return /*#__PURE__*/React.createElement(ButtonLink, buttonProps);
};
FilterButton.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
FilterButton.propTypes = process.env.NODE_ENV !== "production" ? {
  dataName: PropTypes.string,
  ariaLabel: PropTypes.string,
  selected: PropTypes.bool,
  label: PropTypes.string,
  onClick: PropTypes.func
} : {};
const AllCourses = (props, context) => {
  const {
    translate
  } = context;
  const {
    content,
    filters,
    totalContents,
    bannerMicrolearning = undefined,
    search: {
      oldValue: oldSearchValue,
      onChange: handleSearch
    }
  } = props;
  const {
    options,
    onChange
  } = filters;
  const {
    list,
    loading
  } = content;
  const bannerMessageType = bannerMicrolearning?.type;
  const bannerAction = bannerMicrolearning?.action;
  const oldSwitchValue = bannerMicrolearning?.oldSwitchValue;
  const [showCompleted, setShowCompleted] = useState(true);
  const [searchValue, setSearchValue] = useState(oldSearchValue || '');
  const filteredContent = useMemo(() => {
    return showCompleted ? list : _filter(course => course.progress < 1, list);
  }, [list, showCompleted]);
  const debounceHandleSearch = useMemo(() => {
    return _debounce(800, value => {
      handleSearch(value);
    });
  }, [handleSearch]);
  const onSearchChange = useCallback(value => {
    setSearchValue(value);
    debounceHandleSearch(value);
  }, [debounceHandleSearch]);
  const handleSearchReset = useCallback(() => {
    setSearchValue('');
    debounceHandleSearch('');
  }, [debounceHandleSearch]);
  const handleShowCompletedToggle = useCallback(() => {
    setShowCompleted(prevShowCompleted => !prevShowCompleted);
  }, []);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: style.continueLearningWrapper
  }, /*#__PURE__*/React.createElement("span", {
    className: style.continueLearningTitle
  }, translate('all_content')), /*#__PURE__*/React.createElement("span", {
    className: style.continueLearningNumber
  }, _isNil(totalContents) ? _size(filteredContent) : totalContents)), /*#__PURE__*/React.createElement("div", {
    className: style.searchAndSortSection
  }, /*#__PURE__*/React.createElement("div", {
    className: style.searchWrapper
  }, /*#__PURE__*/React.createElement(SearchForm, {
    search: {
      placeholder: translate('search_place_holder'),
      value: searchValue,
      onChange: onSearchChange
    },
    onReset: handleSearchReset,
    dataTestId: "all-courses-search-input"
  })), /*#__PURE__*/React.createElement("div", {
    className: style.sortSection
  }, /*#__PURE__*/React.createElement(InputSwitch, {
    id: 'show-completed-courses-switch',
    name: translate('show_completed'),
    title: translate('show_completed'),
    theme: "default",
    "aria-label": 'Show completed courses aria label',
    value: showCompleted,
    onChange: handleShowCompletedToggle
  }))), !_isEmpty(bannerMicrolearning) ? /*#__PURE__*/React.createElement("div", {
    className: style.bannerWrapper
  }, /*#__PURE__*/React.createElement(Banner, {
    type: "info",
    message: bannerMessageType === 'skill' ? translate('banner_microlearning_rule_message_skill') : translate('banner_microlearning_rule_message'),
    cta: [{
      type: 'switch',
      label: translate('banner_microlearning_rule_label'),
      action: bannerAction,
      oldSwitchValue
    }]
  })) : null, /*#__PURE__*/React.createElement("div", {
    className: style.filterWrapper
  }, _size(options) > 2 ? uncappedMap((filterProps, index) => {
    const {
      name,
      value,
      selected
    } = filterProps;
    function handleFilterChange() {
      onChange && onChange(value);
    }
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      className: style.filterButtonWrapper
    }, /*#__PURE__*/React.createElement(FilterButton, {
      dataName: `filter-button-${value}`,
      ariaLabel: `Filter by ${name}`,
      selected: selected,
      label: name,
      onClick: handleFilterChange
    }), value === 'ALL' ? /*#__PURE__*/React.createElement("div", {
      className: style.divider
    }) : null);
  }, options) : null), /*#__PURE__*/React.createElement("div", null, _size(filteredContent) ? /*#__PURE__*/React.createElement(CardsGrid, {
    list: filteredContent,
    loading: loading,
    customStyle: {
      justifyContent: 'left'
    }
  }) : /*#__PURE__*/React.createElement("div", {
    className: style.emptySearchResultContainer
  }, /*#__PURE__*/React.createElement("div", {
    className: style.emptySearchResultTitle
  }, translate('empty_search_result_title', {
    searchValue
  })), /*#__PURE__*/React.createElement("div", {
    className: style.emptySearchResultDescription
  }, translate('empty_search_result_description')), /*#__PURE__*/React.createElement("div", {
    className: style.emptySearchResultClearSearch,
    onClick: handleSearchReset
  }, translate('empty_search_result_clear_search')))));
};
AllCourses.contextTypes = {
  skin: Provider.childContextTypes.skin,
  translate: Provider.childContextTypes.translate
};
AllCourses.propTypes = process.env.NODE_ENV !== "production" ? {
  content: PropTypes.shape(CardsGrid.propTypes),
  totalContents: PropTypes.number,
  filters: PropTypes.shape({
    onChange: PropTypes.func,
    options: PropTypes.arrayOf(PropTypes.shape(SelectOptionPropTypes))
  }),
  bannerMicrolearning: PropTypes.shape({
    type: PropTypes.oneOf(['skill', 'playlist']),
    action: PropTypes.func,
    oldSwitchValue: PropTypes.bool
  }),
  search: PropTypes.shape({
    oldValue: PropTypes.string,
    onChange: PropTypes.func
  })
} : {};
export default AllCourses;
//# sourceMappingURL=all-courses.js.map