"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.EditorSmartLinkPageObject = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _inProductTesting = require("@atlaskit/in-product-testing");
var _inProduct = require("@atlaskit/smart-card/in-product");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var EditorSmartLinkPageObject = exports.EditorSmartLinkPageObject = /*#__PURE__*/function (_InProductTestPageObj) {
  (0, _inherits2.default)(EditorSmartLinkPageObject, _InProductTestPageObj);
  var _super = _createSuper(EditorSmartLinkPageObject);
  function EditorSmartLinkPageObject(cy, editor) {
    var _this;
    (0, _classCallCheck2.default)(this, EditorSmartLinkPageObject);
    _this = _super.call(this, cy);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "testIds", {
      viewSwitcher: 'link-toolbar-appearance-button',
      displayUrlOption: 'url-appearance',
      displayInlineOption: 'inline-appearance',
      displayCardOption: 'block-appearance',
      displayEmbedOption: 'embed-appearance',
      linkUrl: 'link-url',
      linkLabel: 'link-text'
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "ariaLabels", {
      editLink: 'Edit link',
      deleteLink: 'Remove',
      unlinkLink: 'Unlink'
    });
    _this.cy = cy;
    _this.editor = editor;
    return _this;
  }
  (0, _createClass2.default)(EditorSmartLinkPageObject, [{
    key: "getViewSwitcher",
    value: function getViewSwitcher() {
      return this.cy.get(this.toTestId(this.testIds.viewSwitcher));
    }
  }, {
    key: "openViewSwitcher",
    value: function openViewSwitcher() {
      return this.getViewSwitcher().click();
    }
  }, {
    key: "getViewSwitcherOption",
    value: function getViewSwitcherOption() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      switch (type) {
        case 'url':
          {
            return this.cy.get(this.toTestId(this.testIds.displayUrlOption));
          }
        case 'inline':
          {
            return this.cy.get(this.toTestId(this.testIds.displayInlineOption));
          }
        case 'block':
          {
            return this.cy.get(this.toTestId(this.testIds.displayCardOption));
          }
        case 'embed':
          {
            return this.cy.get(this.toTestId(this.testIds.displayEmbedOption));
          }
        default:
          {
            throw Error("Attempted to switch to `".concat(type, "`: unknown Smart Link view switcher option!"));
          }
      }
    }
  }, {
    key: "selectViewSwitcherOption",
    value: function selectViewSwitcherOption() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      var viewSwitcherOption = this.getViewSwitcherOption(type);
      return viewSwitcherOption.click();
    }
  }, {
    key: "insertSmartLinkByTyping",
    value: function insertSmartLinkByTyping(url) {
      var _this2 = this;
      return this.editor.getEditorArea().type("".concat(url, " "), {
        delay: 0
      }).get('p>a').type('{leftArrow}{leftArrow}').get('[aria-label="Floating Toolbar"]').then(function () {
        _this2.openViewSwitcher();
        _this2.selectViewSwitcherOption('inline');
      });
    }
  }, {
    key: "switchAfterInsert",
    value: function switchAfterInsert() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      // NOTE: inline inserted by default for all Smart Links.
      var inlineCard = new _inProduct.InlineCardPageObject(this.cy);
      inlineCard.click();
      this.openViewSwitcher();
      this.selectViewSwitcherOption(type);
    }
  }, {
    key: "getSmartLink",
    value: function getSmartLink() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      switch (type) {
        case 'inline':
          {
            return new _inProduct.InlineCardPageObject(this.cy);
          }
        case 'block':
          {
            return new _inProduct.BlockCardPageObject(this.cy);
          }
        case 'embed':
          {
            return new _inProduct.EmbedCardPageObject(this.cy);
          }
        case 'url':
          {
            return new _inProduct.InlineCardPageObject(this.cy);
          }
      }
    }
  }, {
    key: "getEditLinkButton",
    value: function getEditLinkButton() {
      return this.cy.get(this.toAriaLabel(this.ariaLabels.editLink));
    }
  }, {
    key: "openEditLinkMenu",
    value: function openEditLinkMenu() {
      var button = this.getEditLinkButton();
      return button.click();
    }
  }, {
    key: "changeLinkLabel",
    value: function changeLinkLabel(title) {
      var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'inline';
      var smartLink = this.getSmartLink(type);
      smartLink.click();
      this.openEditLinkMenu();
      this.typeIntoLabelField(title);
    }
  }, {
    key: "changeLinkUrl",
    value: function changeLinkUrl(url) {
      var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'inline';
      this.getSmartLink(type).click();
      this.openEditLinkMenu();
      this.typeIntoUrlField(url);
    }
  }, {
    key: "typeIntoLabelField",
    value: function typeIntoLabelField(title) {
      var titleField = this.cy.get(this.toTestId(this.testIds.linkLabel)).focus();
      titleField.clear();
      titleField.type("".concat(title, "{enter}"), {
        delay: 0
      });
    }
  }, {
    key: "typeIntoUrlField",
    value: function typeIntoUrlField(url) {
      var urlField = this.cy.get(this.toTestId(this.testIds.linkUrl)).focus();
      urlField.clear();
      urlField.type("".concat(url, "{enter}"), {
        delay: 0
      });
    }
  }, {
    key: "deleteSmartLink",
    value: function deleteSmartLink() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      this.getSmartLink(type).click();
      this.clickDeleteIcon();
    }
  }, {
    key: "clickDeleteIcon",
    value: function clickDeleteIcon() {
      var ariaLabelSelector = this.toAriaLabel(this.ariaLabels.deleteLink);
      var deleteButtonSelector = "button".concat(ariaLabelSelector);
      return this.cy.get(deleteButtonSelector).click();
    }
  }, {
    key: "unlink",
    value: function unlink() {
      var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'inline';
      this.getSmartLink(type).click();
      this.clickUnlinkIcon();
    }
  }, {
    key: "clickUnlinkIcon",
    value: function clickUnlinkIcon() {
      var ariaLabelSelector = this.toAriaLabel(this.ariaLabels.unlinkLink);
      var unlinkButtonSelector = "button".concat(ariaLabelSelector);
      return this.cy.get(unlinkButtonSelector).click();
    }
  }]);
  return EditorSmartLinkPageObject;
}(_inProductTesting.InProductTestPageObject);