/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ "./src/utils/API.ts":
/*!**************************!*\
  !*** ./src/utils/API.ts ***!
  \**************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "REACT_APP_WEATHER_API_KEY": () => (/* binding */ REACT_APP_WEATHER_API_KEY),
/* harmony export */   "fetchWeatherData": () => (/* binding */ fetchWeatherData)
/* harmony export */ });
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
let REACT_APP_WEATHER_API_KEY = '0f52057383af4a36b7b182759232503';
function fetchWeatherData(city) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = yield fetch(`https://api.weatherapi.com/v1/current.json?key=${REACT_APP_WEATHER_API_KEY}&q=${city}&aqi=yes`);
        if (!res.ok) {
            throw new Error('city no found');
        }
        const data = yield res.json();
        return data;
    });
}


/***/ }),

/***/ "./src/utils/messages.ts":
/*!*******************************!*\
  !*** ./src/utils/messages.ts ***!
  \*******************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Messages": () => (/* binding */ Messages)
/* harmony export */ });
var Messages;
(function (Messages) {
    Messages[Messages["TOGGLE_OVERLAY"] = 0] = "TOGGLE_OVERLAY";
    Messages[Messages["UPDATE_TEMPSCALE"] = 1] = "UPDATE_TEMPSCALE";
    Messages[Messages["FETCH_WEATHER_DATA_FROM_BG"] = 2] = "FETCH_WEATHER_DATA_FROM_BG";
    Messages[Messages["UPDATE_BADGE"] = 3] = "UPDATE_BADGE";
})(Messages || (Messages = {}));


/***/ }),

/***/ "./src/utils/storage.ts":
/*!******************************!*\
  !*** ./src/utils/storage.ts ***!
  \******************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "getStoredCities": () => (/* binding */ getStoredCities),
/* harmony export */   "getStoredOptions": () => (/* binding */ getStoredOptions),
/* harmony export */   "setStoredCities": () => (/* binding */ setStoredCities),
/* harmony export */   "setStoredOptions": () => (/* binding */ setStoredOptions)
/* harmony export */ });
function setStoredCities(cities) {
    const vals = {
        cities
    };
    return new Promise(resolve => {
        chrome.storage.local.set(vals, () => {
            resolve();
        });
    });
}
function getStoredCities() {
    const keys = ["cities"];
    return new Promise(resolve => {
        chrome.storage.local.get(keys, (res) => {
            var _a;
            resolve((_a = res.cities) !== null && _a !== void 0 ? _a : []);
        });
    });
}
function setStoredOptions(options) {
    const vals = {
        options,
    };
    return new Promise(resolve => {
        chrome.storage.local.set(vals, () => {
            resolve();
        });
    });
}
function getStoredOptions() {
    const keys = ['options'];
    return new Promise(resolve => {
        chrome.storage.local.get(keys, (res) => {
            resolve(res.options);
        });
    });
}


/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/define property getters */
/******/ 	(() => {
/******/ 		// define getter functions for harmony exports
/******/ 		__webpack_require__.d = (exports, definition) => {
/******/ 			for(var key in definition) {
/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 				}
/******/ 			}
/******/ 		};
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ 	})();
/******/ 	
/******/ 	/* webpack/runtime/make namespace object */
/******/ 	(() => {
/******/ 		// define __esModule on exports
/******/ 		__webpack_require__.r = (exports) => {
/******/ 			if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 				Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 			}
/******/ 			Object.defineProperty(exports, '__esModule', { value: true });
/******/ 		};
/******/ 	})();
/******/ 	
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/*!**************************************!*\
  !*** ./src/background/background.ts ***!
  \**************************************/
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _utils_API__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/API */ "./src/utils/API.ts");
/* harmony import */ var _utils_messages__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/messages */ "./src/utils/messages.ts");
/* harmony import */ var _utils_storage__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/storage */ "./src/utils/storage.ts");



// TODO: background script
chrome.runtime.onInstalled.addListener(() => {
    //setting default values
    (0,_utils_storage__WEBPACK_IMPORTED_MODULE_2__.setStoredCities)([]);
    (0,_utils_storage__WEBPACK_IMPORTED_MODULE_2__.setStoredOptions)({
        tempScale: 'metric',
        homeCity: '',
        hasAutoOverlay: false
    });
    chrome.contextMenus.create({
        contexts: ['selection'],
        title: "Add city to weather extension",
        id: 'weatherExtension'
    });
    chrome.alarms.create({
        periodInMinutes: 60,
    });
});
chrome.runtime.onMessage.addListener(function ({ message, city }, sender, sendResponse) {
    if (message === _utils_messages__WEBPACK_IMPORTED_MODULE_1__.Messages.FETCH_WEATHER_DATA_FROM_BG) {
        console.log("Received request for data from content script");
        // make an API call and return the data to the content script
        fetch(`https://api.weatherapi.com/v1/current.json?key=${_utils_API__WEBPACK_IMPORTED_MODULE_0__.REACT_APP_WEATHER_API_KEY}&q=${city}&aqi=yes`)
            .then(response => response.json())
            .then(data => {
            sendResponse(data);
        })
            .catch(error => {
            console.error("bgscript Error making API call:", error);
            sendResponse({ error: error.message });
        });
        return true; // indicate that sendResponse will be called asynchronously
    }
    else if (message === _utils_messages__WEBPACK_IMPORTED_MODULE_1__.Messages.UPDATE_BADGE) {
        udpateBadge();
        sendResponse('badge updated');
    }
});
//outside of onIntalled since context menu listeners should be install after service woker wakes up 
chrome.contextMenus.onClicked.addListener(e => {
    (0,_utils_storage__WEBPACK_IMPORTED_MODULE_2__.getStoredCities)().then(cities => {
        (0,_utils_storage__WEBPACK_IMPORTED_MODULE_2__.setStoredCities)([...cities, e.selectionText]);
    });
});
chrome.alarms.onAlarm.addListener(() => {
    udpateBadge();
});
function udpateBadge() {
    (0,_utils_storage__WEBPACK_IMPORTED_MODULE_2__.getStoredOptions)().then(options => {
        if (options.homeCity === "")
            return;
        (0,_utils_API__WEBPACK_IMPORTED_MODULE_0__.fetchWeatherData)(options.homeCity).then(data => {
            chrome.action.setBadgeText({
                text: options.tempScale === 'metric' ? Math.round(data.current.temp_c) + '\u2103' : Math.round(data.current.temp_f) + '\u2109'
            });
        });
    });
}

})();

/******/ })()
;
//# sourceMappingURL=background.js.map