/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
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 WeatherPart_1;
import { DataPart } from '../data/data.js';
import { part } from '../../decorators.js';
import { transformLegacyWeather } from './legacy.js';
import { join } from '../../../util/path.js';
const emojiMap = {
    '01d': '\u2600\uFE0F',
    '02d': '\uD83C\uDF24',
    '03d': '\uD83C\uDF25',
    '04d': '\u2601\uFE0F',
    '09d': '\uD83C\uDF27',
    '10d': '\uD83C\uDF26',
    '11d': '\u26C8 ',
    '13d': '\uD83C\uDF28',
    '50d': '\uD83C\uDF2B',
};
var WeatherType;
(function (WeatherType) {
    WeatherType["CLOUDY"] = "cloudy";
    WeatherType["RAINY"] = "rainy";
    WeatherType["SUNNY"] = "sunny";
    WeatherType["SNOWY"] = "snowy";
})(WeatherType || (WeatherType = {}));
let WeatherPart = WeatherPart_1 = class WeatherPart extends DataPart {
    constructor() {
        super(...arguments);
        this._location = 'London, U.K.';
        this._units = 'metric';
    }
    static get defaultLocation() { return 'London, U.K.'; }
    static transformLegacy(app) {
        transformLegacyWeather(app);
    }
    query() {
        const url = join(DataPart.getBaseUrl(), `/weather-city/?q=${encodeURIComponent(this._location)}&units=${encodeURIComponent(this._units)}`);
        return fetch(url)
            .then(r => r.json())
            .then((r) => {
            return r.value;
        });
    }
    is(type) {
        if (!this.value) {
            return false;
        }
        const weather = this.value.weather[0];
        if (!weather) {
            return false;
        }
        switch (type) {
            case WeatherType.SUNNY: {
                return weather.id === 800;
            }
            case WeatherType.RAINY: {
                return weather.id >= 500 && weather.id < 600;
            }
            case WeatherType.SNOWY: {
                return weather.id >= 600 && weather.id < 700;
            }
            case WeatherType.CLOUDY: {
                return weather.id >= 801 && weather.id < 900;
            }
        }
    }
    get temperature() {
        if (!this.value) {
            return NaN;
        }
        return this.value.main.temp;
    }
    get windSpeed() {
        if (!this.value) {
            return NaN;
        }
        return this.value.wind.speed;
    }
    get windDirection() {
        if (!this.value) {
            return NaN;
        }
        return this.value.wind.deg;
    }
    get clouds() {
        if (!this.value) {
            return NaN;
        }
        return this.value.clouds.all;
    }
    get emoji() {
        if (!this.value) {
            return '\u2600\uFE0F';
        }
        const weather = this.value.weather[0];
        if (!weather) {
            return false;
        }
        let icon = weather.icon ? weather.icon : '01d';
        return emojiMap[icon] || '\u2600\uFE0F';
    }
    set location(v) {
        this._location = v;
    }
    get location() {
        return WeatherPart_1.defaultLocation;
    }
    serialize() {
        const data = super.serialize();
        data.units = this._units;
        return data;
    }
    load(data) {
        super.load(data);
        this._units = data.units || 'metric';
    }
};
WeatherPart = WeatherPart_1 = __decorate([
    part('weather')
], WeatherPart);
export { WeatherPart };
//# sourceMappingURL=weather.js.map