"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _fdx = require("@whalecloud/fdx");

var _react = _interopRequireDefault(require("react"));

var _IconBase = require("../../assets/IconBase64");

var _LinkModal = _interopRequireDefault(require("../../composite-comp/dito/config-panels/components/LinkModal"));

var _BottomButton = _interopRequireDefault(require("../components/BottomButton"));

var _MPropertiesBaseComponent = _interopRequireDefault(require("../MPropertiesBaseComponent"));

require("./index.less");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys.push.apply(ownKeys, Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var MCatalogMenuProperties = /*#__PURE__*/function (_MPropertiesBaseCompo) {
  _inherits(MCatalogMenuProperties, _MPropertiesBaseCompo);

  var _super = _createSuper(MCatalogMenuProperties);

  function MCatalogMenuProperties(_props) {
    var _this;

    _classCallCheck(this, MCatalogMenuProperties);

    _this = _super.call(this, _props);

    _defineProperty(_assertThisInitialized(_this), "resetProps", function (props) {
      _this.setState(_objectSpread({}, props, {
        catalogList: props.catalogList ? props.catalogList : [],
        firstCatalogNmae: props.firstCatalogNmae ? 'All Categories' : '',
        maxCount: props.maxCount ? props.maxCount : 10
      }));
    });

    _defineProperty(_assertThisInitialized(_this), "closeModal", function () {
      return _this.setState({
        visible: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "showModal", function (index) {
      _this.setState({
        visible: true,
        itemIndex: index
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleModalSave", function (url) {
      _this.setCatalogItemLink(_this.state.itemIndex, url);

      _this.closeModal();
    });

    _defineProperty(_assertThisInitialized(_this), "addNewItem", function () {
      _this.setState({
        catalogList: [].concat(_toConsumableArray(_this.state.catalogList), [{}])
      });
    });

    _defineProperty(_assertThisInitialized(_this), "delItem", function (index) {
      var arr = JSON.parse(JSON.stringify(_this.state.catalogList));
      arr.splice(index, 1);

      _this.setState({
        catalogList: arr
      });
    });

    _defineProperty(_assertThisInitialized(_this), "setCatalogItemName", function (index, name) {
      var arr = JSON.parse(JSON.stringify(_this.state.catalogList));
      arr[index].name = name;

      _this.setState({
        catalogList: arr
      });
    });

    _defineProperty(_assertThisInitialized(_this), "setCatalogItemLink", function (index, link) {
      var arr = JSON.parse(JSON.stringify(_this.state.catalogList));
      arr[index].link = link;

      _this.setState({
        catalogList: arr
      });
    });

    _defineProperty(_assertThisInitialized(_this), "renderLink", function (item, index) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "fullUrl",
        key: index
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "urlInp"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Input, {
        placeholder: "Please enter the name",
        value: item.name,
        onChange: function onChange(event) {
          event.persist();

          _this.setCatalogItemName(index, event.target.value);
        },
        style: {
          height: '28px',
          padding: '4px 20px 4px 11px'
        }
      }), item.name && /*#__PURE__*/_react["default"].createElement("div", {
        className: "butCot"
      }, /*#__PURE__*/_react["default"].createElement("img", {
        onClick: function onClick() {
          return _this.setCatalogItemName(index, '');
        },
        className: "closeBut",
        width: "14",
        src: _IconBase.closeIcon,
        alt: ""
      }))), /*#__PURE__*/_react["default"].createElement("div", {
        className: "delIcon"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
        onClick: function onClick() {
          return _this.showModal(index);
        },
        style: {
          padding: '0 10px',
          height: '28px',
          marginRight: '4px'
        }
      }, item.link ? /*#__PURE__*/_react["default"].createElement("img", {
        height: 16,
        src: _IconBase.linkIcon,
        alt: ""
      }) : /*#__PURE__*/_react["default"].createElement("img", {
        height: 16,
        src: _IconBase.unlinkIcon,
        alt: ""
      })), /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
        onClick: function onClick() {
          return _this.delItem(index);
        },
        style: {
          padding: '0 10px',
          height: '28px'
        }
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Icon, {
        type: "delete"
      }))));
    });

    _defineProperty(_assertThisInitialized(_this), "onSave", function () {
      var evt = new Event('property-update', {
        bubbles: false
      });
      evt.data = _objectSpread({}, _this.props, _this.state);
      window.document.dispatchEvent(evt);
    });

    _this.state = _objectSpread({
      showType: 'display',
      visible: false,
      itemIndex: 0,
      firstCatalogNmae: 'All Categories',
      catalogList: [],
      maxCount: 10
    }, _props);
    return _this;
  }

  _createClass(MCatalogMenuProperties, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$state = this.state,
          showType = _this$state.showType,
          firstCatalogNmae = _this$state.firstCatalogNmae,
          catalogList = _this$state.catalogList,
          visible = _this$state.visible,
          maxCount = _this$state.maxCount;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "catalogMenuWrap"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Form, {
        layout: "vertical"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Form.Item, {
        label: "Category Navigation"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Radio.Group, {
        value: showType
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Radio, {
        value: "display"
      }, "Display"), /*#__PURE__*/_react["default"].createElement(_fdx.Radio, {
        value: "hidden"
      }, "Hidden"))), /*#__PURE__*/_react["default"].createElement(_fdx.Row, {
        className: "imgTitle",
        type: "flex",
        justify: "space-between"
      }, /*#__PURE__*/_react["default"].createElement("div", null, "Page Navigation"), /*#__PURE__*/_react["default"].createElement("div", null, catalogList.length + 1, "/", maxCount)), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_fdx.Input, {
        value: firstCatalogNmae,
        onChange: function onChange(event) {
          event.persist();

          _this2.setState({
            firstCatalogNmae: event.target.value
          });
        },
        placeholder: "Please enter the name",
        style: {
          height: '28px',
          padding: '4px 20px 4px 11px',
          marginBottom: '10px'
        }
      })), catalogList.map(function (item, index) {
        return _this2.renderLink(item, index);
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: "addBut"
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Button, {
        style: {
          width: '100%'
        },
        onClick: function onClick() {
          if (catalogList.length + 1 >= maxCount) {
            return;
          }

          _this2.addNewItem();
        }
      }, /*#__PURE__*/_react["default"].createElement(_fdx.Icon, {
        type: "plus"
      }), " Add Navigation")), /*#__PURE__*/_react["default"].createElement(_BottomButton["default"], {
        onSaveClick: function onSaveClick() {
          return _this2.onSave();
        }
      })), /*#__PURE__*/_react["default"].createElement(_LinkModal["default"], {
        visible: visible,
        closeModal: this.closeModal,
        handleModalSave: this.handleModalSave
      }));
    }
  }]);

  return MCatalogMenuProperties;
}(_MPropertiesBaseComponent["default"]);

exports["default"] = MCatalogMenuProperties;