/// <reference path="Cookie.ts" />
/// <reference path="./ICookieConfigOptions.ts" />
var econda;
(function (econda) {
    var cookie;
    (function (cookie_1) {
        /**
         * Helper to read and write cookies.
         *
         * @class econda.cookie.Store
         * @static
         *
         * <h3>Example</h3>
         * <p>Add new cookie to browser:</p>
         *      econda.cookie.Store.set({name: "myCookie", value: "my cookie value"});
         *
         * <p>Get value of existing cookie (or null if not set):</p>
         *      var val = econda.cookie.Store.getValue("myCookie");
         *
         * <p>Delete cookie:</p>
         *      econda.cookie.Store.remove({name: "mycookie", domain: ".example.com"});
         */
        var Store = (function () {
            function Store() {
            }
            /**
             * True if a cookie with given name exists
             * @param {String} cookieName
             * @returns {Boolean}
             */
            Store.contains = function (cookieName) {
                if (typeof document.cookie === 'undefined') {
                    return false;
                }
                return new RegExp("(?:;\\s*|^)" + encodeURIComponent(cookieName) + '=').test(document.cookie);
            };
            /**
             * Find cookies matching given regular expression. Will return key/value only (no additional properties like domain,...).
             * @param {RegExp} query
             * @returns {Object} Object with properties containing all matching cookie values
             */
            Store.find = function (query) {
                if (typeof document.cookie === 'undefined') {
                    return {};
                }
                var pairs = document.cookie.split(';'), pair, result = {};
                for (var index = 0, len = pairs.length; index < len; ++index) {
                    pair = pairs[index].split('=');
                    var name = pair[0].replace(/^\s+|\s+$/, '');
                    var value = pair.slice(1).join('=');
                    if ((query instanceof RegExp) == false || query.test(name))
                        result[decodeURIComponent(name)] = decodeURIComponent(value);
                }
                return result;
            };
            /**
             * Get value for cookie by name or null if cookie was not found.
             * @param {String} name
             * @returns {String} Value of cookie
             */
            Store.getValue = function (name) {
                if (econda.cookie.Store.contains(name)) {
                    return econda.cookie.Store.find(name)[name];
                }
                else {
                    return null;
                }
            };
            /**
             * Set / add cookie to browsers cookie collection
             *
             *     econda.cookie.Store.set({name: "mycookie", value: "example"});
             *
             * @param {econda.cookie.Cookie/Object} cookie object or cookie config options
             * @param {Boolean} [encodeValue=true] encodeURIComponent cookie value
             * @chainable
             */
            Store.set = function (data, encodeValue) {
                if (encodeValue === void 0) { encodeValue = true; }
                if (typeof document.cookie === 'undefined') {
                    return this;
                }
                var cookie = new econda.cookie.Cookie(data);
                var name = cookie.getName();
                var value = cookie.getValue();
                var path = cookie.getPath();
                var domain = cookie.getDomain();
                var expires = cookie.getExpires();
                var secure = cookie.getSecure();
                var def = [encodeURIComponent(name) + '=' + (encodeValue ? encodeURIComponent(value) : value)];
                if (path)
                    def.push('path=' + path);
                if (domain)
                    def.push('domain=' + domain);
                if (expires)
                    def.push('expires=' + expires.toUTCString());
                if (secure)
                    def.push('secure');
                document.cookie = def.join(';');
                return this;
            };
            /**
             * Remove cookie from browsers cookie collection. Make sure to remove a cookie with the same settings as it was set before.
             * You'll not be able to remove a cookie by name, if it was set with domain and / or path settings.
             *
             *     econda.cookie.Store.remove({name: "mycookie", domain: ".example.com"});
             *
             * @param {econda.cookie.Cookie/Object/String} cookie object or cookie config options
             * @chainable
             */
            Store.remove = function (data) {
                if (typeof document.cookie === 'undefined') {
                    return this;
                }
                var cookie;
                if (typeof data === 'string') {
                    cookie = new econda.cookie.Cookie({ name: data });
                }
                else {
                    cookie = new econda.cookie.Cookie(data);
                }
                var name = cookie.getName();
                var path = cookie.getPath();
                var domain = cookie.getDomain();
                var secure = cookie.getSecure();
                var def = [encodeURIComponent(name) + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'];
                if (path)
                    def.push('path=' + path);
                if (domain)
                    def.push('domain=' + domain);
                if (secure)
                    def.push('secure');
                document.cookie = def.join(';');
                return this;
            };
            return Store;
        }());
        cookie_1.Store = Store;
    })(cookie = econda.cookie || (econda.cookie = {}));
})(econda || (econda = {}));