function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }

function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }

function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }

function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }

function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }

function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }

function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }

function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }

function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }

function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }

function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }

function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }

(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["default~cc-policies-cc-policies-configuration-cc-polocies-configuration-module~cc-pools-cc-pools-module"], {
  /***/
  "./src/app/pages/home/cloud-cost/cc-pools/service/cc-pools.service.ts":
  /*!****************************************************************************!*\
    !*** ./src/app/pages/home/cloud-cost/cc-pools/service/cc-pools.service.ts ***!
    \****************************************************************************/

  /*! exports provided: CcPoolsService */

  /***/
  function _src_app_pages_home_cloudCost_ccPools_service_ccPoolsServiceTs(module, __webpack_exports__, __webpack_require__) {
    "use strict";

    __webpack_require__.r(__webpack_exports__);
    /* harmony export (binding) */


    __webpack_require__.d(__webpack_exports__, "CcPoolsService", function () {
      return CcPoolsService;
    });
    /* harmony import */


    var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
    /*! tslib */
    "./node_modules/tslib/tslib.es6.js");
    /* harmony import */


    var _angular_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
    /*! @angular/core */
    "./node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js");
    /* harmony import */


    var rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
    /*! rxjs/Subject */
    "./node_modules/rxjs-compat/_esm2015/Subject.js");
    /* harmony import */


    var src_app_core_session_session_service__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(
    /*! src/app/core/session/session.service */
    "./src/app/core/session/session.service.ts");
    /* harmony import */


    var src_app_core_store_store__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(
    /*! src/app/core/store/store */
    "./src/app/core/store/store.ts");
    /* harmony import */


    var src_environments_environment__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(
    /*! src/environments/environment */
    "./src/environments/environment.ts");
    /* harmony import */


    var _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(
    /*! ./cc-pools.state */
    "./src/app/pages/home/cloud-cost/cc-pools/service/cc-pools.state.ts");

    var CcPoolsService = /*#__PURE__*/function (_src_app_core_store_s) {
      function CcPoolsService(sessionService) {
        var _this;

        _classCallCheck(this, CcPoolsService);

        _this = _callSuper(this, CcPoolsService);
        _this.sessionService = sessionService;
        return _this;
      }

      _inherits(CcPoolsService, _src_app_core_store_s);

      return _createClass(CcPoolsService, [{
        key: "addPoolsConfiguration",
        value: function addPoolsConfiguration(payload) {
          var output = new rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__["Subject"]();
          var path = src_environments_environment__WEBPACK_IMPORTED_MODULE_5__["environment"].api.cloudCostOptimization.add.endpoint;
          var basePath = this.sessionService.getBasePath(true, null, null);
          setTimeout(function () {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolsLoadingState"]());
          }, 0);
          this.controller.post(path, payload, basePath).subscribe(function (data) {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolssLoadedState"](data));
            output.complete();
          }, function (error) {
            console.log("erro in storing pools data", error);
            output.error(error);
            output.complete();
          });
          return output;
        }
      }, {
        key: "readPoolsConfiguration",
        value: function readPoolsConfiguration() {
          var output = new rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__["Subject"]();
          var path = src_environments_environment__WEBPACK_IMPORTED_MODULE_5__["environment"].api.cloudCostOptimization.read.endpoint;
          var basePath = this.sessionService.getBasePath(true, null, null);
          setTimeout(function () {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolsLoadingState"]());
          }, 0);
          this.controller.get(path, null, basePath).subscribe(function (data) {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolssLoadedState"](data));
            output.complete();
          }, function (error) {
            console.log("erro in storing pools data", error);
            output.error(error);
            output.complete();
          });
          return output;
        } // deletePoolsConfiguration(id): Observable<Store.State>{
        //     const output = new Subject<Store.State>();
        //     let path = environment.api.cloudCostOptimization.delete.endpoint;
        //     path = path + '/' + id['$oid']
        //     let basePath = this.sessionService.getBasePath(true, null, null);
        //     setTimeout(() => {
        //         output.next(new CCPoolsLoadingState)
        //     }, 0);
        //    this.controller.delete(path,basePath).subscribe(
        //     (data) => {
        //         output.next(new CCPoolssLoadedState(data));
        //         output.complete();
        //     },
        //     (error:any) => {
        //         console.log("erro in storing pools data", error)
        //         output.error(error);
        //         output.complete();
        //     }
        //    );
        //    return output;
        // }

      }, {
        key: "deletePoolsConfiguration",
        value: function deletePoolsConfiguration(id) {
          var output = new rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__["Subject"]();
          var path = src_environments_environment__WEBPACK_IMPORTED_MODULE_5__["environment"].api.cloudCostOptimization["delete"].endpoint; // handle both string and object

          var oid = typeof id === 'string' ? id : id === null || id === void 0 ? void 0 : id.$oid;
          path = path + '/' + oid;
          console.log("deletePoolsConfiguration;;;;;;;", path);
          var basePath = this.sessionService.getBasePath(true, null, null);
          setTimeout(function () {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolsLoadingState"]());
          }, 0);
          this.controller["delete"](path, basePath).subscribe(function (data) {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolssLoadedState"](data));
            output.complete();
          }, function (error) {
            console.log("erro in storing pools data", error);
            output.error(error);
            output.complete();
          });
          return output;
        }
      }, {
        key: "getCurrentMonthCostData",
        value: function getCurrentMonthCostData() {
          var output = new rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__["Subject"]();
          var path = src_environments_environment__WEBPACK_IMPORTED_MODULE_5__["environment"].api.cloudResources.read.endpoint;
          var basePath = this.sessionService.getBasePath(true, null, null);
          var now = new Date(); // Start of the current month (1st day at 00:00:00 UTC)

          var startDateISO = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)).toISOString(); // End of today (current day at 23:59:59 UTC)

          var endDateISO = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 23, 59, 59)).toISOString();
          var payload = [{
            $lookup: {
              from: "cloud_cost_visibility",
              "let": {
                resource_id: "$resourceId"
              },
              pipeline: [{
                $match: {
                  $expr: {
                    $and: [{
                      $eq: ["$resource_id", "$$resource_id"]
                    }, {
                      $gte: ["$start_date", startDateISO]
                    }, {
                      $lte: ["$end_date", endDateISO]
                    }]
                  }
                }
              }],
              as: "costs"
            }
          }, {
            $match: {
              PoolName: {
                $ne: null
              }
            }
          }, {
            $addFields: {
              // Sum all costs for this resource in the period
              resourceCost: {
                $sum: "$costs.cost"
              }
            }
          }, {
            $project: {
              costs: 0 // Hide the costs array if needed

            }
          }, {
            $group: {
              _id: "$PoolName",
              totalCost: {
                $sum: "$resourceCost"
              },
              resources: {
                $push: "$$ROOT"
              }
            }
          }]; // Emit loading state

          setTimeout(function () {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolsLoadingState"]());
          }, 0); // Make API call

          this.controller.post(path, payload, basePath).subscribe(function (data) {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolssLoadedState"](data));
            console.log("Current Month Start Date (local):", startDateISO.toLocaleString());
            console.log("Current Month Start Date:", startDateISO);
            console.log("End Date:", endDateISO);
            output.complete();
          }, function (error) {
            console.error("Pool cost aggregation error:", error);
            output.error(error);
            output.complete();
          });
          return output;
        }
      }, {
        key: "getPreviousAndCurrentMonthDateRange",
        value: function getPreviousAndCurrentMonthDateRange() {
          var now = new Date(); // Start of previous month (1st day)

          var startDate = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - 1, 1)); // End of current day in current month (23:59:59)

          var endDate = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 23, 59, 59));
          return {
            startDate: startDate,
            endDate: endDate
          };
        }
      }, {
        key: "getPreviousAndCurrentMonthCostData",
        value: function getPreviousAndCurrentMonthCostData() {
          var output = new rxjs_Subject__WEBPACK_IMPORTED_MODULE_2__["Subject"]();
          var path = src_environments_environment__WEBPACK_IMPORTED_MODULE_5__["environment"].api.cloudResources.read.endpoint;
          var basePath = this.sessionService.getBasePath(true, null, null);

          var _this$getPreviousAndC = this.getPreviousAndCurrentMonthDateRange(),
              startDate = _this$getPreviousAndC.startDate,
              endDate = _this$getPreviousAndC.endDate;

          var startDateISO = startDate.toISOString();
          var endDateISO = endDate.toISOString();
          var payload = [{
            $lookup: {
              from: "cloud_cost_visibility",
              "let": {
                resource_id: "$resourceId"
              },
              pipeline: [{
                $match: {
                  $expr: {
                    $and: [{
                      $eq: ["$resource_id", "$$resource_id"]
                    }, {
                      $gte: ["$start_date", startDateISO]
                    }, {
                      $lte: ["$end_date", endDateISO]
                    }]
                  }
                }
              }],
              as: "costs"
            }
          }, {
            $match: {
              PoolName: {
                $ne: null
              }
            }
          }, {
            $addFields: {
              // Sum all costs for this resource in the period
              resourceCost: {
                $sum: "$costs.cost"
              },
              // costStartDate: { $arrayElemAt: ["$costs.start_date", 0] }
              costStartDate: {
                $min: "$costs.start_date"
              }
            }
          }, {
            $project: {
              costs: 0 // Hide the costs array if needed

            }
          }, {
            $group: {
              _id: {
                poolName: "$PoolName"
              },
              minStartDate: {
                $min: "$costStartDate"
              },
              totalCost: {
                $sum: "$resourceCost"
              },
              resources: {
                $push: "$$ROOT"
              }
            }
          }]; // Emit loading state

          setTimeout(function () {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolsLoadingState"]());
          }, 0); // Make API call

          this.controller.post(path, payload, basePath).subscribe(function (data) {
            output.next(new _cc_pools_state__WEBPACK_IMPORTED_MODULE_6__["CCPoolssLoadedState"](data));
            console.log("Previous Month Start Date (local):", startDateISO.toLocaleString());
            console.log("Previous Month Start Date:", startDateISO);
            console.log("End Date:", endDateISO);
            output.complete();
          }, function (error) {
            console.error("Pool cost aggregation error:", error);
            output.error(error);
            output.complete();
          });
          return output;
        }
      }]);
    }(src_app_core_store_store__WEBPACK_IMPORTED_MODULE_4__["Store"].AbstractService);

    CcPoolsService.ctorParameters = function () {
      return [{
        type: src_app_core_session_session_service__WEBPACK_IMPORTED_MODULE_3__["SessionService"]
      }];
    };

    CcPoolsService = Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__decorate"])([Object(_angular_core__WEBPACK_IMPORTED_MODULE_1__["Injectable"])({
      providedIn: 'root'
    })], CcPoolsService);
    /***/
  },

  /***/
  "./src/app/pages/home/cloud-cost/cc-pools/service/cc-pools.state.ts":
  /*!**************************************************************************!*\
    !*** ./src/app/pages/home/cloud-cost/cc-pools/service/cc-pools.state.ts ***!
    \**************************************************************************/

  /*! exports provided: CCPoolsLoadingState, CCPoolssLoadedState, CCPoolsErrorState */

  /***/
  function _src_app_pages_home_cloudCost_ccPools_service_ccPoolsStateTs(module, __webpack_exports__, __webpack_require__) {
    "use strict";

    __webpack_require__.r(__webpack_exports__);
    /* harmony export (binding) */


    __webpack_require__.d(__webpack_exports__, "CCPoolsLoadingState", function () {
      return CCPoolsLoadingState;
    });
    /* harmony export (binding) */


    __webpack_require__.d(__webpack_exports__, "CCPoolssLoadedState", function () {
      return CCPoolssLoadedState;
    });
    /* harmony export (binding) */


    __webpack_require__.d(__webpack_exports__, "CCPoolsErrorState", function () {
      return CCPoolsErrorState;
    });
    /* harmony import */


    var src_app_core_store_store__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
    /*! src/app/core/store/store */
    "./src/app/core/store/store.ts");

    var CCPoolsLoadingState = /*#__PURE__*/function (_src_app_core_store_s2) {
      function CCPoolsLoadingState() {
        _classCallCheck(this, CCPoolsLoadingState);

        return _callSuper(this, CCPoolsLoadingState, arguments);
      }

      _inherits(CCPoolsLoadingState, _src_app_core_store_s2);

      return _createClass(CCPoolsLoadingState);
    }(src_app_core_store_store__WEBPACK_IMPORTED_MODULE_0__["Store"].AbstractLoadingState);

    ;

    var CCPoolssLoadedState = /*#__PURE__*/function (_src_app_core_store_s3) {
      function CCPoolssLoadedState() {
        _classCallCheck(this, CCPoolssLoadedState);

        return _callSuper(this, CCPoolssLoadedState, arguments);
      }

      _inherits(CCPoolssLoadedState, _src_app_core_store_s3);

      return _createClass(CCPoolssLoadedState);
    }(src_app_core_store_store__WEBPACK_IMPORTED_MODULE_0__["Store"].AbstractIdealState);

    ;

    var CCPoolsErrorState = /*#__PURE__*/function (_src_app_core_store_s4) {
      function CCPoolsErrorState() {
        _classCallCheck(this, CCPoolsErrorState);

        return _callSuper(this, CCPoolsErrorState, arguments);
      }

      _inherits(CCPoolsErrorState, _src_app_core_store_s4);

      return _createClass(CCPoolsErrorState);
    }(src_app_core_store_store__WEBPACK_IMPORTED_MODULE_0__["Store"].AbstractErrorState);

    ;
    /***/
  }
}]);