/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */

import {LitElement, html, css, unsafeCSS} from 'lit';
import {customElement, property, query} from 'lit/decorators.js';
import { breakpoints } from '../../breakpoints';

@customElement('iu-paragraph')

export class Paragraph extends LitElement {
  static override styles = css`
    :host{
      display: block;
      margin-bottom: var(--iu-spacing-block-sm); 
    }
    @media ${unsafeCSS(breakpoints.md)} {
      .column-1{
        width: calc(50% - var(--iu-grid-gutter));
      }
    }
    .fs-1 {
      --iu-p-f: var(--iu-f-1);
    }
    .fs-2 p{
      --iu-p-f: var(--iu-f-2);
    }
    @media ${unsafeCSS(breakpoints.md)} {
      .fs-3{
        --iu-p-f: var(--iu-f-2);
      }
    }
    @media ${unsafeCSS(breakpoints.xl)} {
      .fs-3{
        --iu-p-f: var(--iu-f-4);
      }
    }
    ::slotted(p){
      font: var(--iu-p-f) !important;
      margin-bottom: 1.125rem !important;
      margin-top: 0;
    }
  `;

  @property({type: Number}) columns = 2;
  @property({type: Number}) size = 1;

  @property({type: Boolean, reflect: true}) nested?: boolean = false;

  @query('slot') slotElement!: HTMLSlotElement;

  wrapListItems(){
    const lists = this.querySelectorAll('ul, ol');
    lists.forEach((list) => {
      list.classList.add('list')
    })
    const items = this.querySelectorAll('li')
    items.forEach(item => {
      const content = item.innerHTML
      const newContent = `<span>${content}</span>`
      item.innerHTML = newContent
    })
  }

  override firstUpdated(){
    this.wrapListItems()
  }

  override render() {
    return html`
      <iu-container class="${this.nested ? 'is-nested' : ''}">
        <div class="inner fs-${this.size} column-${this.columns}">
          <slot></slot>
        </div>
      </iu-container>
    `;
  }

}

declare global {
  interface HTMLElementTagNameMap {
    'iu-paragraph': Paragraph;
  }
}