function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React, { useMemo, useState } from 'react';
import { PisellModal, Icon } from '@pisell/materials';
import { locales } from '@pisell/utils';
import NoteBlock from "../../../components/booking/notes/NoteBlock";
import { textStyles } from "../hooks/perspectiveUtils";
var normalizeInternalNotes = function normalizeInternalNotes(value) {
  if (!value) return [];
  if (Array.isArray(value)) {
    return value.map(function (item, index) {
      if (typeof item === 'string' || typeof item === 'number') {
        return {
          id: index,
          content: String(item)
        };
      }
      if (item && _typeof(item) === 'object') {
        return item;
      }
      return {
        id: index,
        content: String(item)
      };
    }).filter(function (item) {
      return (item === null || item === void 0 ? void 0 : item.content) != null && (item === null || item === void 0 ? void 0 : item.content) !== '';
    });
  }
  if (typeof value === 'string' || typeof value === 'number') {
    return [{
      id: 0,
      content: String(value)
    }];
  }
  return [];
};
var BookingNotesPreview = function BookingNotesPreview(props) {
  var dataSource = props.dataSource,
    labels = props.labels,
    style = props.style;
  var orderNote = typeof (dataSource === null || dataSource === void 0 ? void 0 : dataSource.note) === 'string' ? dataSource.note.trim() : '';
  var internalNotes = useMemo(function () {
    return normalizeInternalNotes(dataSource === null || dataSource === void 0 ? void 0 : dataSource.internalNotes);
  }, [dataSource === null || dataSource === void 0 ? void 0 : dataSource.internalNotes]);
  var displayLines = useMemo(function () {
    var lines = [];
    if (orderNote) {
      lines.push({
        label: (labels === null || labels === void 0 ? void 0 : labels.orderNote) || locales.getText('pisell2.salesManagement.booking.notes.orderNotes'),
        content: orderNote
      });
    }
    internalNotes.forEach(function (item) {
      var content = typeof (item === null || item === void 0 ? void 0 : item.content) === 'string' ? item.content.trim() : '';
      if (!content) return;
      lines.push({
        label: (labels === null || labels === void 0 ? void 0 : labels.internalNote) || locales.getText('pisell2.salesManagement.booking.notes.internalNotes'),
        content: content
      });
    });
    return lines;
  }, [orderNote, internalNotes, labels === null || labels === void 0 ? void 0 : labels.orderNote, labels === null || labels === void 0 ? void 0 : labels.internalNote]);
  var visibleLines = displayLines.slice(0, 2);
  var showMore = displayLines.length > 0;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    modalOpen = _useState2[0],
    setModalOpen = _useState2[1];
  if (!displayLines.length) {
    return null;
  }
  var openNotesModal = function openNotesModal(event) {
    event.stopPropagation();
    setModalOpen(true);
  };
  var orderNotes = orderNote ? [{
    id: 0,
    content: orderNote
  }] : [];
  var isEditable = Boolean(dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis);
  return /*#__PURE__*/React.createElement("div", {
    style: style,
    onClick: function onClick(e) {
      return e.stopPropagation();
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      flexDirection: 'column',
      cursor: 'pointer'
    },
    onClick: openNotesModal
  }, visibleLines.map(function (line, index) {
    return /*#__PURE__*/React.createElement("div", {
      key: "".concat(line.label, "-").concat(index),
      style: _objectSpread(_objectSpread({}, textStyles.text), {}, {
        fontSize: 14,
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        textOverflow: 'ellipsis'
      })
    }, /*#__PURE__*/React.createElement("span", null, line.label, ":"), ' ', /*#__PURE__*/React.createElement("span", {
      style: {
        color: '#eaa96f'
      }
    }, line.content));
  }), showMore ? /*#__PURE__*/React.createElement("div", {
    style: {
      color: '#7f56d9',
      display: 'flex',
      alignItems: 'center'
    }
  }, (labels === null || labels === void 0 ? void 0 : labels.more) || locales.getText('pisell2.salesManagement.booking.view-more'), /*#__PURE__*/React.createElement(Icon, {
    size: 20,
    type: "pisell2-chevron-right-double"
  })) : null), /*#__PURE__*/React.createElement(PisellModal, {
    open: modalOpen,
    title: locales.getText('pisell2.salesManagement.booking.notes.title'),
    width: 600,
    footer: null,
    onCancel: function onCancel() {
      return setModalOpen(false);
    },
    destroyOnClose: true,
    bodyStyle: {
      overflowX: 'hidden'
    }
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      display: 'flex',
      flexDirection: 'column',
      gap: 16
    }
  }, /*#__PURE__*/React.createElement(NoteBlock, {
    noteType: "order",
    maxLength: 1,
    title: "".concat(locales.getText('pisell2.salesManagement.booking.notes.orderNotes'), ":"),
    describe: "",
    notes: orderNotes,
    apis: dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis,
    placeholder: locales.getText('pisell2.salesManagement.booking.notes.addOrderNotes'),
    isEdit: isEditable,
    onChange: dataSource === null || dataSource === void 0 ? void 0 : dataSource.onChange
  }), (internalNotes.length > 0 || isEditable) && /*#__PURE__*/React.createElement(NoteBlock, {
    maxLength: 99,
    noteType: "internal",
    title: "".concat(locales.getText('pisell2.salesManagement.booking.notes.internalNotes'), ":"),
    describe: locales.getText('pisell2.salesManagement.booking.notes.internalVisibleOnlyTip'),
    placeholder: locales.getText('pisell2.salesManagement.booking.notes.addInternalNotes'),
    notes: internalNotes,
    isEdit: isEditable,
    apis: dataSource === null || dataSource === void 0 ? void 0 : dataSource.apis,
    bookingId: dataSource === null || dataSource === void 0 ? void 0 : dataSource.bookingId,
    onChange: dataSource === null || dataSource === void 0 ? void 0 : dataSource.onChange
  }))));
};
export default BookingNotesPreview;