"use strict";

exports.__esModule = true;
exports.default = void 0;
var _debounce2 = _interopRequireDefault(require("lodash/fp/debounce"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _isNil2 = _interopRequireDefault(require("lodash/fp/isNil"));
var _size2 = _interopRequireDefault(require("lodash/fp/size"));
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _filter2 = _interopRequireDefault(require("lodash/fp/filter"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _select = require("../../atom/select");
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _searchForm = _interopRequireDefault(require("../../molecule/search-form"));
var _cardsGrid = _interopRequireDefault(require("../../organism/cards-grid"));
var _inputSwitch = _interopRequireDefault(require("../../atom/input-switch"));
var _banner = _interopRequireDefault(require("../../molecule/banner"));
var _colors = require("../../variables/colors");
var _allCourses = _interopRequireDefault(require("./all-courses.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// @ts-expect-error convert untypped
const uncappedMap = _map2.default.convert({
  cap: false
});
const FilterButton = (props, context) => {
  const {
    dataName,
    ariaLabel,
    selected,
    label,
    onClick
  } = props;
  const {
    skin
  } = context;
  const primarySkinColor = (0, _get2.default)('common.primary', skin);
  const buttonProps = {
    customStyle: {
      backgroundColor: selected ? primarySkinColor : _colors.COLORS.cm_grey_150,
      color: selected ? _colors.COLORS.white : _colors.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.default.createElement(_buttonLink.default, buttonProps);
};
FilterButton.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
FilterButton.propTypes = process.env.NODE_ENV !== "production" ? {
  dataName: _propTypes.default.string,
  ariaLabel: _propTypes.default.string,
  selected: _propTypes.default.bool,
  label: _propTypes.default.string,
  onClick: _propTypes.default.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] = (0, _react.useState)(true);
  const [searchValue, setSearchValue] = (0, _react.useState)(oldSearchValue || '');
  const filteredContent = (0, _react.useMemo)(() => {
    return showCompleted ? list : (0, _filter2.default)(course => course.progress < 1, list);
  }, [list, showCompleted]);
  const debounceHandleSearch = (0, _react.useMemo)(() => {
    return (0, _debounce2.default)(800, value => {
      handleSearch(value);
    });
  }, [handleSearch]);
  const onSearchChange = (0, _react.useCallback)(value => {
    setSearchValue(value);
    debounceHandleSearch(value);
  }, [debounceHandleSearch]);
  const handleSearchReset = (0, _react.useCallback)(() => {
    setSearchValue('');
    debounceHandleSearch('');
  }, [debounceHandleSearch]);
  const handleShowCompletedToggle = (0, _react.useCallback)(() => {
    setShowCompleted(prevShowCompleted => !prevShowCompleted);
  }, []);
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.continueLearningWrapper
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: _allCourses.default.continueLearningTitle
  }, translate('all_content')), /*#__PURE__*/_react.default.createElement("span", {
    className: _allCourses.default.continueLearningNumber
  }, (0, _isNil2.default)(totalContents) ? (0, _size2.default)(filteredContent) : totalContents)), /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.searchAndSortSection
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.searchWrapper
  }, /*#__PURE__*/_react.default.createElement(_searchForm.default, {
    search: {
      placeholder: translate('search_place_holder'),
      value: searchValue,
      onChange: onSearchChange
    },
    onReset: handleSearchReset,
    dataTestId: "all-courses-search-input"
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.sortSection
  }, /*#__PURE__*/_react.default.createElement(_inputSwitch.default, {
    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
  }))), !(0, _isEmpty2.default)(bannerMicrolearning) ? /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.bannerWrapper
  }, /*#__PURE__*/_react.default.createElement(_banner.default, {
    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.default.createElement("div", {
    className: _allCourses.default.filterWrapper
  }, (0, _size2.default)(options) > 2 ? uncappedMap((filterProps, index) => {
    const {
      name,
      value,
      selected
    } = filterProps;
    function handleFilterChange() {
      onChange && onChange(value);
    }
    return /*#__PURE__*/_react.default.createElement("div", {
      key: index,
      className: _allCourses.default.filterButtonWrapper
    }, /*#__PURE__*/_react.default.createElement(FilterButton, {
      dataName: `filter-button-${value}`,
      ariaLabel: `Filter by ${name}`,
      selected: selected,
      label: name,
      onClick: handleFilterChange
    }), value === 'ALL' ? /*#__PURE__*/_react.default.createElement("div", {
      className: _allCourses.default.divider
    }) : null);
  }, options) : null), /*#__PURE__*/_react.default.createElement("div", null, (0, _size2.default)(filteredContent) ? /*#__PURE__*/_react.default.createElement(_cardsGrid.default, {
    list: filteredContent,
    loading: loading,
    customStyle: {
      justifyContent: 'left'
    }
  }) : /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.emptySearchResultContainer
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.emptySearchResultTitle
  }, translate('empty_search_result_title', {
    searchValue
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.emptySearchResultDescription
  }, translate('empty_search_result_description')), /*#__PURE__*/_react.default.createElement("div", {
    className: _allCourses.default.emptySearchResultClearSearch,
    onClick: handleSearchReset
  }, translate('empty_search_result_clear_search')))));
};
AllCourses.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
AllCourses.propTypes = process.env.NODE_ENV !== "production" ? {
  content: _propTypes.default.shape(_cardsGrid.default.propTypes),
  totalContents: _propTypes.default.number,
  filters: _propTypes.default.shape({
    onChange: _propTypes.default.func,
    options: _propTypes.default.arrayOf(_propTypes.default.shape(_select.SelectOptionPropTypes))
  }),
  bannerMicrolearning: _propTypes.default.shape({
    type: _propTypes.default.oneOf(['skill', 'playlist']),
    action: _propTypes.default.func,
    oldSwitchValue: _propTypes.default.bool
  }),
  search: _propTypes.default.shape({
    oldValue: _propTypes.default.string,
    onChange: _propTypes.default.func
  })
} : {};
var _default = exports.default = AllCourses;
//# sourceMappingURL=all-courses.js.map