"use strict";

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

var _Page = _interopRequireDefault(require("./Page"));

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

class CreatePage extends _Page.default {
  constructor() {
    super();
    this.url = '/create';
    this.selector = '.cspace-ui-CreatePage--common';
  }

  hasRecordType(recordTypeDescriptor) {
    const link = this.getRecordLink(recordTypeDescriptor);
    return !!link && link.isVisible();
  }

  hasVocabulary(vocabularyDescriptor) {
    const link = this.getVocabularyLink(vocabularyDescriptor);
    return !!link && link.isVisible();
  }

  getRecordLink(recordTypeDescriptor) {
    const {
      serviceType,
      name,
      displayName
    } = recordTypeDescriptor;
    const container = typeof serviceType !== 'undefined' ? browser.$(this.selector).$(".cspace-ui-CreatePagePanel--".concat(serviceType)) : browser.$(this.selector);

    if (!container) {
      return null;
    }

    const links = container.$$("*[id=\"".concat(name, "\"]"));

    if (links.length === 0) {
      return null;
    }

    const link = links[0];

    if (typeof displayName !== 'undefined' && link.getText() !== displayName) {
      return null;
    }

    if (typeof name !== 'undefined' && typeof serviceType !== 'undefined' && serviceType !== 'authority') {
      const url = "/record/".concat(name);
      const href = link.getAttribute('href');

      if (!href || !href.endsWith(url)) {
        return null;
      }
    }

    return link;
  }

  getVocabularyLink(vocabularyDescriptor) {
    const {
      authorityName,
      name,
      displayName
    } = vocabularyDescriptor;
    const recordLink = this.getRecordLink({
      name: authorityName,
      serviceType: 'authority'
    });

    if (!recordLink) {
      return null;
    }

    const container = recordLink.$('..');

    if (!container) {
      return null;
    }

    const links = container.$$("a[id=\"".concat(authorityName, "/").concat(name, "\"]"));

    if (links.length === 0) {
      return null;
    }

    const link = links[0];

    if (typeof displayName !== 'undefined' && link.getText() !== displayName) {
      return null;
    }

    if (typeof name !== 'undefined') {
      const url = "/record/".concat(authorityName, "/").concat(name);
      const href = link.getAttribute('href');

      if (!href || !href.endsWith(url)) {
        return null;
      }
    }

    return link;
  }

}

exports.default = CreatePage;