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 React, { useState } from 'react';
import { DeleteOutlined, EditOutlined, EllipsisOutlined, HistoryOutlined, MenuFoldOutlined, MenuUnfoldOutlined, PlusSquareOutlined, VerticalAlignTopOutlined } from '@ant-design/icons';
import { Dropdown, Input, Modal } from 'antd';
import { Menu } from 'antd';
import styles from "./index.module.less";
import { useSnapshot } from 'valtio';
import { AIChatStore, createPureNewSession, handleDeleteSession, handlePinSession, handleRenameSession } from "../../../../model/AIChat";
import { useSetState } from "ahooks";
import { useIntl } from 'dumi';
import { isUUID } from "../../../../utils";
export var ConversationsMenu = function ConversationsMenu() {
  var _useSetState = useSetState({
      open: false,
      session: null,
      rename: ''
    }),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    collapsed = _useState2[0],
    setCollapsed = _useState2[1];
  var snap = useSnapshot(AIChatStore);
  var handleSelectSession = function handleSelectSession(sessionId) {
    if (isUUID(sessionId)) {
      AIChatStore.activeSessionId = sessionId;
    }
  };
  var toggleCollapsed = function toggleCollapsed() {
    setCollapsed(!collapsed);
  };
  var _useIntl = useIntl(),
    formatMessage = _useIntl.formatMessage;
  var items = [{
    key: 'fold',
    extra: !collapsed ? /*#__PURE__*/React.createElement(MenuFoldOutlined, null) : null,
    onClick: toggleCollapsed,
    label: null,
    icon: !collapsed ? null : /*#__PURE__*/React.createElement(MenuUnfoldOutlined, null),
    title: collapsed ? formatMessage({
      id: 'ai.conversations.expand'
    }) : formatMessage({
      id: 'ai.conversations.collapse'
    })
  }, {
    key: 'new',
    icon: /*#__PURE__*/React.createElement(PlusSquareOutlined, null),
    label: formatMessage({
      id: 'ai.conversations.new'
    }),
    onClick: function onClick() {
      return createPureNewSession();
    }
  }, {
    key: 'history',
    label: formatMessage({
      id: 'ai.conversations.history'
    }),
    icon: /*#__PURE__*/React.createElement(HistoryOutlined, null),
    children: snap.sessions.map(function (session) {
      return {
        key: session.id,
        label: /*#__PURE__*/React.createElement("div", {
          className: styles.menuItem
        }, /*#__PURE__*/React.createElement("span", {
          className: styles.title
        }, session.title), /*#__PURE__*/React.createElement(Dropdown, {
          menu: {
            items: [{
              key: 'edit',
              label: formatMessage({
                id: 'ai.conversations.rename'
              }),
              icon: /*#__PURE__*/React.createElement(EditOutlined, null),
              onClick: function onClick(_ref) {
                var domEvent = _ref.domEvent;
                domEvent.stopPropagation();
                setState({
                  open: true,
                  session: session,
                  rename: session.title.slice(0, 100)
                });
              }
            }, {
              key: 'top',
              label: formatMessage({
                id: 'ai.conversations.pin'
              }),
              icon: /*#__PURE__*/React.createElement(VerticalAlignTopOutlined, null),
              onClick: function onClick(_ref2) {
                var domEvent = _ref2.domEvent;
                domEvent.stopPropagation();
                handlePinSession(session.id);
              }
            }, {
              key: 'delete',
              label: formatMessage({
                id: 'ai.conversations.delete'
              }),
              icon: /*#__PURE__*/React.createElement(DeleteOutlined, null),
              onClick: function onClick(_ref3) {
                var domEvent = _ref3.domEvent;
                domEvent.stopPropagation();
                handleDeleteSession(session.id);
              }
            }]
          },
          trigger: ['click']
        }, /*#__PURE__*/React.createElement("span", {
          className: styles.iconWrapper,
          onClick: function onClick(e) {
            e.preventDefault();
            e.stopPropagation();
          }
        }, /*#__PURE__*/React.createElement(EllipsisOutlined, null))))
      };
    })
  }];
  return /*#__PURE__*/React.createElement("div", {
    className: styles.container
  }, /*#__PURE__*/React.createElement(Menu, {
    selectedKeys: [snap.activeSessionId],
    defaultOpenKeys: ['history'],
    mode: "inline",
    theme: "light",
    inlineCollapsed: collapsed,
    items: items,
    onSelect: function onSelect(_ref4) {
      var key = _ref4.key;
      return handleSelectSession(key);
    }
  }), /*#__PURE__*/React.createElement(Modal, {
    title: formatMessage({
      id: 'ai.conversations.edit.title'
    }),
    open: state.open,
    centered: true,
    maskClosable: false,
    onOk: function onOk() {
      handleRenameSession(state.session.id, state.rename);
      setState({
        open: false
      });
    },
    onCancel: function onCancel() {
      return setState({
        open: false
      });
    },
    okButtonProps: {
      disabled: !state.rename
    },
    okText: formatMessage({
      id: 'ai.conversations.ok'
    }),
    cancelText: formatMessage({
      id: 'ai.conversations.cancel'
    })
  }, /*#__PURE__*/React.createElement(Input, {
    showCount: true,
    maxLength: 100,
    onChange: function onChange(e) {
      return setState({
        rename: e.target.value
      });
    },
    value: state.rename
  })));
};