import { Breakpoints } from '../config/Breakpoints';

export class PictureController {

  private imagesStatic: any;
  public sizes: Array<string> = Object.keys(Breakpoints);

  constructor(
    private $element
  ) {}

  public get images(): any {
    let images = {};
    if (this.imagesStatic) {
      images = this.imagesStatic;
    } else {
      let imagesAttribute = this.$element[0].parentNode.getAttribute('images');
      if (imagesAttribute) {
        images = JSON.parse(imagesAttribute);
      }
    }
    return images;
  }

  public set images(images: any) {
    if (images) {
      if (images.constructor.name === 'String') {
        try {
          this.imagesStatic = JSON.parse(images);
        } catch (error) {
          console.error(error);
        }
      } else {
        this.imagesStatic = images;
      }
    } else {
      let parentAttribute = this.$element[0].parentNode.getAttribute('images');
      if (parentAttribute) {
        this.imagesStatic = JSON.parse(parentAttribute);
      } else {
        this.imagesStatic = {};
      }
    }
  }
}
