/**
 * Created by hubert on 8/06/17.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import "polymer/polymer-element.html";
import "iron-flex-layout/iron-flex-layout.html";
import "ozone-item-preview";
import "ozone-api-item";
import "ozone-collection";
import "./ozone-mosaic.html";
import 'ozone-iron-list';
import { customElement, domElement, property } from 'taktik-polymer-typescript';
import { SearchQuery } from "ozone-search-helper";
/**
 * <ozone-mosaic> is an element that display ozone items in a mosaic view.
 *
 * ```html
 * <ozone-mosaic item-data={{item}}>  </ozone-mosaic>
 * ```
 *
 *  ### Events
 *
 * * *results-found* Fired when results are found by the API.
 *
 * ### Implements
 *
 *  *TaktikSearchApiBehavior*
 *
 *  ### Mixin
 *  Custom property | Description | Default
 *  ----------------|-------------|----------
 *  `--ozone-mosaic-item`  | css mixin for the list of item | `{ box-sizing: border-box;width:300px;height:200px;margin:10px;display:flex;overflow: hidden;}`
 *  `--ozone-mosaic-item-margins` | css mixin for the margin of previewContainer | `default 8px`
 */
let OzoneMosaic = class OzoneMosaic extends Polymer.Element {
    constructor() {
        super(...arguments);
        /**
         * id of the source
         */
        this.searchResults = [];
        /**
         * string to search in the collection
         */
        this.searchString = '';
        /**
         * true indicate that all the data data still available with this search.
         */
        this.hasMoreData = false;
        /**
         * Define size of query result
         * @type {number}
         */
        this.searchSize = 50;
        /**
         * type of the collection
         */
        this.collectionType = 'item';
        this._scrollTrigger = false;
        /**
         * scrollthreshold in percent to trigger loading of more items
         * @type {number}
         */
        this.scrollthresholdPc = 80;
    }
    _collectionTypeChange(collectionType) {
        this.$.ironList.$.mosaicCollection.set('collection', collectionType);
    }
    clearTriggers() {
        this._scrollTrigger = false;
    }
    ready() {
        super.ready();
        this.$.ironList
            .addEventListener('collection-property-changed', (event) => {
            this.set(event.detail.name, event.detail.value);
        });
    }
    /**
     * trigger quickSearch in the collection
     * @param searchString
     */
    searchInItems(searchString) {
        if (typeof (searchString) !== 'undefined') {
            this.set('searchResults', []);
            return this.$.ironList.$.mosaicCollection.quickSearch(searchString, this.searchSize);
        }
        return Promise.reject('Undefined search String');
    }
    /**
     * start a new search base on -searchString-.
     */
    requestSearch() {
        this.searchInItems(this.searchString);
    }
    /**
     * Save given item.
     * @param {Item} updatedData
     * @return {Promise<Item>}
     */
    saveSelectedItem(updatedData) {
        if (updatedData) {
            return this.$.ironList.$.mosaicCollection.saveOne(updatedData).then((index) => {
                return this.searchResults[index];
            });
        }
        else {
            return Promise.reject('updatedData is null');
        }
    }
    /**
     * perform a custom search
     * @param {SearchRequest} requestQuery
     * @return {Promise<Array<Item>>}
     */
    customSearchQuery(requestQuery) {
        const searchQuery = new SearchQuery();
        searchQuery.custom(requestQuery);
        return this.search(searchQuery);
    }
    /**
     * start search query
     * @param {SearchQuery} searchRequest
     * @return {Promise<Array<Item>>}
     */
    search(searchRequest) {
        return this.$.ironList.$.mosaicCollection.search(searchRequest);
    }
    /**
     * empty collection
     */
    clear() {
        return this.$.ironList.clear();
    }
};
__decorate([
    domElement(),
    __metadata("design:type", Object)
], OzoneMosaic.prototype, "$", void 0);
__decorate([
    property({
        type: Array,
        notify: true,
    }),
    __metadata("design:type", Array)
], OzoneMosaic.prototype, "searchResults", void 0);
__decorate([
    property({ type: String }),
    __metadata("design:type", String)
], OzoneMosaic.prototype, "searchString", void 0);
__decorate([
    property({
        type: Number,
        notify: true,
    }),
    __metadata("design:type", Number)
], OzoneMosaic.prototype, "total", void 0);
__decorate([
    property({
        type: Boolean,
        notify: true,
    }),
    __metadata("design:type", Boolean)
], OzoneMosaic.prototype, "hasMoreData", void 0);
__decorate([
    property({ type: Number }),
    __metadata("design:type", Object)
], OzoneMosaic.prototype, "searchSize", void 0);
__decorate([
    property({
        type: String,
        observer: '_collectionTypeChange',
    }),
    __metadata("design:type", String)
], OzoneMosaic.prototype, "collectionType", void 0);
__decorate([
    property({ type: Number }),
    __metadata("design:type", Object)
], OzoneMosaic.prototype, "scrollthresholdPc", void 0);
OzoneMosaic = __decorate([
    customElement('ozone-mosaic')
], OzoneMosaic);
export { OzoneMosaic };
