function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
import React, { useContext, useMemo } from "react";
import { Context } from "../model";
import { locales } from "@pisell/utils";
import NoteBlock from "../../notes/NoteBlock";
var Notes = function Notes() {
  var _modal$state3, _modal$state6;
  var modal = useContext(Context);
  var orderNote = useMemo(function () {
    var _modal$state, _modal$state2;
    if (!((_modal$state = modal.state) !== null && _modal$state !== void 0 && (_modal$state = _modal$state.notes) !== null && _modal$state !== void 0 && _modal$state.orderNote)) {
      return [];
    }
    return [{
      id: 0,
      content: ((_modal$state2 = modal.state) === null || _modal$state2 === void 0 || (_modal$state2 = _modal$state2.notes) === null || _modal$state2 === void 0 ? void 0 : _modal$state2.orderNote) || ""
    }];
  }, [(_modal$state3 = modal.state) === null || _modal$state3 === void 0 || (_modal$state3 = _modal$state3.notes) === null || _modal$state3 === void 0 ? void 0 : _modal$state3.orderNote]);
  var noteChange = function noteChange(_ref) {
    var note = _ref.note,
      status = _ref.status,
      noteType = _ref.noteType;
    if (noteType === "internal") {
      var _modal$state4, _modal$state5, _modal$state5$refresh;
      var _list = _toConsumableArray((_modal$state4 = modal.state) === null || _modal$state4 === void 0 ? void 0 : _modal$state4.notes.internalNotes);
      if (status == "add") {
        _list.push(note);
      }
      if (status == "remove") {
        _list = _list.filter(function (item) {
          return item.id != note.id;
        });
      }
      if (status == "edit") {
        _list = _list.map(function (item) {
          if (item.id == note.id) {
            return note;
          }
          return item;
        });
      }
      modal.dispatch({
        type: "setNotes",
        payload: {
          internalNotes: _list
        }
      });
      (_modal$state5 = modal.state) === null || _modal$state5 === void 0 || (_modal$state5$refresh = _modal$state5.refreshData) === null || _modal$state5$refresh === void 0 || _modal$state5$refresh.call(_modal$state5, {});
      return;
    }
    if (noteType === "order") {
      modal.dispatch({
        type: "setNotes",
        payload: {
          orderNote: note.content || "",
          edit: false
        }
      });
    }
  };
  if (orderNote.length === 0 && modal.state.disableEventBookingEdit) {
    return null;
  }
  return /*#__PURE__*/React.createElement(NoteBlock, {
    noteType: "order",
    maxLength: 1,
    title: "".concat(locales.getText("pisell2.text.order-notes"), ":"),
    describe: "",
    notes: orderNote,
    apis: modal === null || modal === void 0 || (_modal$state6 = modal.state) === null || _modal$state6 === void 0 ? void 0 : _modal$state6.apis,
    placeholder: locales.getText("pisell2.text.add-order-notes"),
    isEdit: false,
    onChange: function onChange(e, type, noteType) {
      noteChange({
        note: e,
        status: type,
        noteType: noteType
      });
    }
  });
};
export default Notes;