import React, { useCallback } from 'react';
import { Dropdown } from 'antd';
import { Iconfont } from '@pisell/materials';
import { locales } from '@pisell/utils';
import { useOrderNoteModal } from "../../../../hooks/useOrderNoteModal";
import "./index.less";
var PREFIX = 'pisell-sale-detail-notes-panel';
function getOrderNoteText(context) {
  var _context$sales$tempOr;
  return ((_context$sales$tempOr = context.sales.tempOrder) === null || _context$sales$tempOr === void 0 || (_context$sales$tempOr = _context$sales$tempOr.note) === null || _context$sales$tempOr === void 0 ? void 0 : _context$sales$tempOr.trim()) || '';
}
function getNoteSections(context) {
  var orderNoteText = getOrderNoteText(context);
  return [{
    key: 'orderNote',
    title: locales.getText('pisell2.sale-detail.order-note'),
    notes: orderNoteText ? [orderNoteText] : []
  }
  // {
  //   key: 'internalNote',
  //   title: locales.getText('pisell2.sale-detail.internal-notes'),
  //   notes: [],
  // },
  ];
}

/**
 * 统计模板 B 备注 tab 的徽标数量；后续新增内部备注时在此追加 section。
 */
export function getSaleDetailTemplateBNotesCount(context) {
  return getNoteSections(context).reduce(function (total, section) {
    return total + section.notes.length;
  }, 0);
}

/**
 * 模板 B 的备注详情面板，沿用订单备注弹窗维护编辑入口。
 */
export function Notes(_ref) {
  var context = _ref.context;
  var sections = getNoteSections(context);
  var orderNoteText = getOrderNoteText(context);
  var submitAndRefresh = useCallback(function () {
    return context.sales.submitAndRefresh();
  }, [context.sales]);
  var _useOrderNoteModal = useOrderNoteModal({
      context: context,
      onAfterChange: submitAndRefresh
    }),
    openOrderNoteModal = _useOrderNoteModal.openOrderNoteModal,
    orderNoteModal = _useOrderNoteModal.orderNoteModal,
    removeOrderNote = _useOrderNoteModal.removeOrderNote;
  var orderNoteMenuItems = [{
    key: 'edit',
    label: locales.getText('pisell2.sale-detail.edit-note')
  }, {
    key: 'remove',
    label: locales.getText('pisell2.sale-detail.remove-note')
  }];
  var handleOrderNoteMenuClick = function handleOrderNoteMenuClick(_ref2) {
    var key = _ref2.key;
    if (key === 'edit') {
      openOrderNoteModal();
      return;
    }
    if (key === 'remove') {
      removeOrderNote();
    }
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("main", {
    className: "pisell-sale-detail-main pisell-sale-detail-notes-panel"
  }, sections.map(function (section) {
    var isOrderNoteSection = section.key === 'orderNote';
    return /*#__PURE__*/React.createElement("section", {
      className: "".concat(PREFIX, "__section"),
      key: section.key
    }, /*#__PURE__*/React.createElement("header", {
      className: "".concat(PREFIX, "__section-header")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__section-title")
    }, /*#__PURE__*/React.createElement("span", null, section.title))), isOrderNoteSection ? /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__list")
    }, orderNoteText ? /*#__PURE__*/React.createElement("article", {
      className: "".concat(PREFIX, "__card")
    }, /*#__PURE__*/React.createElement("p", {
      className: "".concat(PREFIX, "__text")
    }, orderNoteText), /*#__PURE__*/React.createElement(Dropdown, {
      menu: {
        items: orderNoteMenuItems,
        onClick: handleOrderNoteMenuClick
      },
      placement: "bottomRight",
      trigger: ['click']
    }, /*#__PURE__*/React.createElement("button", {
      className: "".concat(PREFIX, "__menu-button"),
      type: "button",
      "aria-label": locales.getText('pisell2.sale-detail.note-actions')
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-dots-vertical"
    })))) : /*#__PURE__*/React.createElement("button", {
      className: "".concat(PREFIX, "__add"),
      type: "button",
      onClick: openOrderNoteModal
    }, locales.getText('pisell2.sale-detail.add-notes'))) : /*#__PURE__*/React.createElement("div", {
      className: "".concat(PREFIX, "__reserved"),
      "aria-hidden": "true"
    }));
  })), orderNoteModal);
}