import Component  from '../component.js'

export default class CoUxLayoutBreadcrumbs extends Component {
    constructor(parent) {
      let selector = '*[@data-e2e="ux-layout-breadcrumbs" or @class="ecl-breadcrumb"]';
      super(parent, selector);
      this.item = new CoUxLayoutBreadcrumbsItem(this.currentPath);
    }
    /**
    * @type boolean
    */
    async isECL() {
      let cl = await (await this.element()).getAttribute('class');
      if (cl.includes('ecl-breadcrumb')) {
         return true;
      } else if (cl.includes('ux-layout-breadcrumbs')) {
         return false;
      } else {
         throw new Error('ux-layout-breadcrumbs component object needs to be revised, root path is missing class');
      }
    }
    /**
    * @type string
    */
    async homeUrl() {
      return await (await this.element()).getAttribute('homeurl');
    }
    /**
    * @type string
    */
    async homeurllabel() {
      return await (await this.element()).getAttribute('homeurllabel');
    }
    /**
    * @type boolean
    */
    async hasSegments() {
      // Home breadcrumb link is only present if the breadcrumb has Segments
      let homeBreadcrumb = new CoUxLayoutBreadcrumbsItem(this.currentPath).byIndex(1);
      return await (await homeBreadcrumb.element()).isExisting();
    }
}

export class CoUxLayoutBreadcrumbsItem extends Component {
    constructor(parent) {
      let selector = 'span[contains(@class,"ux-layout-breadcrumbs__item")]';
      super(parent, selector);
    }
    /**
    * @type WebdriverIO.Element
    */
    get breadcrumbItemLink() {
      return (this.currentPath + '//a');
    }
    /**
    * @type WebdriverIO.Element
    */
    get breadcrumbItemLabel() {
      return (this.currentPath + '//span');
    }

}