function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { CloseOutlined } from '@ant-design/icons';
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { transformRgb } from "../../_util";
import { HistoryIcon } from "../../_util/iconSvgHtml";
import { cancelFavorite, fetchFavorite, saveFavorite } from "../service";
import dark_star from "./assets/dark_star.png";
import light_star from "./assets/light_star.png";
import "./index.less";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
var Header = function Header(props) {
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    hasCollect = _useState2[0],
    setHasCollect = _useState2[1];
  var title = props.title,
    _props$theme = props.theme,
    theme = _props$theme === void 0 ? 'light' : _props$theme,
    type = props.type,
    id = props.id,
    _props$showCollect = props.showCollect,
    showCollect = _props$showCollect === void 0 ? false : _props$showCollect,
    tagList = props.tagList,
    onCancel = props.onCancel,
    openHistory = props.openHistory;
  useEffect(function () {
    if (id && type && showCollect) {
      getFavorite();
    }
  }, [id, type, showCollect]);
  // 获取是否收藏
  var getFavorite = function getFavorite() {
    fetchFavorite({
      objectIds: id,
      collectionTypes: type
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        var _res$data;
        if ((res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.length) > 0) {
          setHasCollect(res.data.includes(id));
        }
      }
    });
  };
  // 取消收藏
  var delFavorite = function delFavorite() {
    cancelFavorite({
      objectIds: id,
      collectionType: type
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        setHasCollect(false);
      }
    });
  };
  // 添加收藏
  var addFavorite = function addFavorite() {
    saveFavorite({
      objectIds: id,
      collectionType: type
    }).then(function (res) {
      if ((res === null || res === void 0 ? void 0 : res.result) === 0) {
        setHasCollect(true);
      }
    });
  };
  // 收藏 取消收藏
  var toggleCollect = function toggleCollect() {
    if (hasCollect) {
      delFavorite();
    } else {
      addFavorite();
    }
  };
  var tagColorMap = {
    blue: '#1677FF',
    green: '#0FB964',
    yellow: '#FFAA00',
    red: '#F5222D'
  };
  if (theme === 'dark') {
    tagColorMap = {
      blue: '#4BAEFF',
      green: '#1CEF86',
      yellow: '#FFAA00',
      red: '#FF5861'
    };
  }
  return /*#__PURE__*/_jsx("div", {
    className: 'vm-modal-header-container',
    children: /*#__PURE__*/_jsxs("div", {
      className: 'vm-modal-header-wrap',
      children: [/*#__PURE__*/_jsxs("div", {
        className: 'vm-modal-header-wrap-left',
        children: [/*#__PURE__*/_jsx("div", {
          className: 'vm-modal-header-wrap-left-title',
          children: title
        }), tagList === null || tagList === void 0 ? void 0 : tagList.filter(function (item) {
          return !!item.value;
        }).map(function (item) {
          var color = tagColorMap[item.color];
          return /*#__PURE__*/_jsx("div", {
            className: 'vm-modal-header-wrap-left-tag',
            style: {
              color: color,
              borderColor: color,
              background: transformRgb(color, 0.15)
            },
            children: item.value
          }, item.value);
        })]
      }), /*#__PURE__*/_jsxs("div", {
        className: 'vm-modal-header-wrap-right',
        children: [(type === 'car' || type === 'staff') && /*#__PURE__*/_jsx(HistoryIcon, {
          width: 16,
          height: 16,
          fill: theme === 'light' ? '#595959' : '#fff',
          onClick: function onClick() {
            return openHistory();
          },
          style: {
            cursor: 'pointer'
          }
        }), showCollect && /*#__PURE__*/_jsx("div", {
          className: classNames('vm-modal-header-wrap-right-collect', hasCollect ? 'active' : ''),
          onClick: function onClick() {
            toggleCollect();
          },
          children: /*#__PURE__*/_jsx("img", {
            src: theme === 'light' ? light_star : dark_star,
            width: 16,
            height: 16
          })
        }), showCollect || type === 'car' || type === 'staff' ? /*#__PURE__*/_jsx("div", {
          className: 'vm-modal-header-wrap-right-line'
        }) : null, /*#__PURE__*/_jsx("div", {
          className: 'vm-modal-header-wrap-right-close',
          onClick: onCancel,
          children: /*#__PURE__*/_jsx(CloseOutlined, {})
        })]
      })]
    })
  });
};
export default Header;