import { Injectable } from '@angular/core';
import { RequestMethod } from '@angular/http';
import { Observable, BehaviorSubject } from 'rxjs/Rx';
import { ApiRoot } from 'dotcms-js/dotcms-js';
import { CoreWebService } from 'dotcms-js/dotcms-js';
var BundleService = (function () {
    function BundleService(_apiRoot, coreWebService) {
        this._apiRoot = _apiRoot;
        this.coreWebService = coreWebService;
        this.bundles$ = new BehaviorSubject([]);
        this._bundlesAry = [];
        this._environmentsAry = [];
        this._bundleStoreUrl = this._apiRoot.baseUrl + "api/bundle/getunsendbundles/userid";
        this._loggedUserUrl = this._apiRoot.baseUrl + "api/v1/users/current/";
        this._addToBundleUrl =
            this._apiRoot.baseUrl + "DotAjaxDirector/com.dotcms.publisher.ajax.RemotePublishAjaxAction/cmd/addToBundle";
        this._pushEnvironementsUrl = this._apiRoot.baseUrl + "api/environment/loadenvironments/roleId";
        this._pushRuleUrl =
            this._apiRoot.baseUrl + "DotAjaxDirector/com.dotcms.publisher.ajax.RemotePublishAjaxAction/cmd/publish";
    }
    BundleService.fromServerBundleTransformFn = function (data) {
        return data.items || [];
    };
    BundleService.fromServerEnvironmentTransformFn = function (data) {
        // Endpoint return extra empty environment
        data.shift();
        return data;
    };
    BundleService.prototype.getLoggedUser = function () {
        return this.coreWebService.request({
            method: RequestMethod.Get,
            url: this._loggedUserUrl,
        });
    };
    BundleService.prototype.loadBundleStores = function () {
        var _this = this;
        var obs = this._doLoadBundleStores().map(function (bundles) {
            _this._bundlesAry = bundles;
            return bundles;
        });
        obs.subscribe(function (bundles) { return _this.bundles$.next(bundles); });
    };
    BundleService.prototype._doLoadBundleStores = function () {
        var _this = this;
        return this.getLoggedUser().flatMap(function (user) {
            return _this.coreWebService.request({
                method: RequestMethod.Get,
                url: _this._bundleStoreUrl + "/" + user.userId,
            }).map(BundleService.fromServerBundleTransformFn);
        });
    };
    BundleService.prototype.loadPublishEnvironments = function () {
        var _this = this;
        var obs;
        if (this._environmentsAry.length) {
            obs = Observable.of(this._environmentsAry);
        }
        else {
            obs = this._doLoadPublishEnvironments().map(function (environments) {
                _this._environmentsAry = environments;
                return environments;
            });
        }
        return obs;
    };
    BundleService.prototype._doLoadPublishEnvironments = function () {
        var _this = this;
        return this.getLoggedUser().flatMap(function (user) {
            return _this.coreWebService.request({
                method: RequestMethod.Get,
                url: _this._pushEnvironementsUrl + "/" + user.roleId + "/?name=0",
            }).map(BundleService.fromServerEnvironmentTransformFn);
        });
    };
    BundleService.prototype.addRuleToBundle = function (ruleId, bundle) {
        return this.coreWebService.request({
            body: "assetIdentifier=" + ruleId + "&bundleName=" + bundle.name + "&bundleSelect=" + bundle.id,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: RequestMethod.Post,
            url: this._addToBundleUrl
        });
    };
    BundleService.prototype.pushPublishRule = function (ruleId, environmentId) {
        return this.coreWebService.request({
            body: this.getPublishRuleData(ruleId, environmentId),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            method: RequestMethod.Post,
            url: this._pushRuleUrl
        });
    };
    BundleService.prototype.getFormattedDate = function (date) {
        var yyyy = date.getFullYear().toString();
        var mm = (date.getMonth() + 1).toString();
        var dd = date.getDate().toString();
        return yyyy + '-' + (mm[1] ? mm : '0' + mm[0]) + '-' + (dd[1] ? dd : '0' + dd[0]);
    };
    BundleService.prototype.getPublishRuleData = function (ruleId, environmentId) {
        var resul = '';
        resul += "assetIdentifier=" + ruleId;
        resul += "&remotePublishDate=" + this.getFormattedDate(new Date());
        resul += '&remotePublishTime=00-00';
        resul += "&remotePublishExpireDate=" + this.getFormattedDate(new Date());
        resul += '&remotePublishExpireTime=00-00';
        resul += '&iWantTo=publish';
        resul += "&whoToSend=" + environmentId;
        resul += '&bundleName=';
        resul += '&bundleSelect=';
        resul += '&forcePush=false';
        return resul;
    };
    return BundleService;
}());
export { BundleService };
BundleService.decorators = [
    { type: Injectable },
];
/** @nocollapse */
BundleService.ctorParameters = function () { return [
    { type: ApiRoot, },
    { type: CoreWebService, },
]; };
//# sourceMappingURL=bundle-service.js.map