'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _Page = require('./Page');

var _Page2 = _interopRequireDefault(_Page);

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

class CreatePage extends _Page2.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 = recordTypeDescriptor.serviceType,
          name = recordTypeDescriptor.name,
          displayName = recordTypeDescriptor.displayName;


    const container = typeof serviceType !== 'undefined' ? browser.$(this.selector).$(`.cspace-ui-CreatePagePanel--${serviceType}`) : browser.$(this.selector);

    if (!container) {
      return null;
    }

    const links = container.$$(`*[id="${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/${name}`;
      const href = link.getAttribute('href');

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

    return link;
  }

  getVocabularyLink(vocabularyDescriptor) {
    const authorityName = vocabularyDescriptor.authorityName,
          name = vocabularyDescriptor.name,
          displayName = vocabularyDescriptor.displayName;


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

    if (!recordLink) {
      return null;
    }

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

    if (!container) {
      return null;
    }

    const links = container.$$(`a[id="${authorityName}/${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/${authorityName}/${name}`;
      const href = link.getAttribute('href');

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

    return link;
  }
}
exports.default = CreatePage;