// LICENSE : MIT
"use strict";
import querystring from "querystring";
import OAuthRequest from "../API/OAuthRequest";
import Consumer from "./HatenaCunsumer";

const Authentication = require("electron").remote.require(__dirname + "/HatenaAuthentication");
export default class HatenaClient {
    isLogin() {
        return Authentication.canAccess();
    }

    loginAsync(callback) {
        return Authentication.requireAccess(callback);
    }

    _hatenaRequest() {
        const credential = Authentication.getCredential();
        return new OAuthRequest({
            consumerKey: Consumer.key,
            consumerSecret: Consumer.secret,
            accessKey: credential.accessKey,
            accessSecret: credential.accessSecret
        });
    }

    getContent(url) {
        let query = querystring.stringify({
            url
        });
        return this._hatenaRequest()
            .get("http://api.b.hatena.ne.jp/1/my/bookmark?" + query)
            .then(response => {
                if (response.statusCode === 403) {
                    const data = JSON.parse(response["data"]);
                    return Promise.reject(new Error(data["message"]));
                }
                return response;
            });
    }

    getTags() {
        return this._hatenaRequest()
            .get("http://api.b.hatena.ne.jp/1/my/tags")
            .then(response => {
                return response.tags.map(tag => tag.tag);
            });
    }

    /**
     *
     * @param options
     * @returns {*}
     * {
            url,
            comment,
            tags = []
        }
     */
    postLink(options = {}) {
        let { url, comment, tags } = options;
        if (tags.length > 0) {
            // [tag][tag] comment
            const tagPrefix = tags
                .map(tag => {
                    return `[${tag}]`;
                })
                .join("");
            comment = tagPrefix + comment;
        }
        let query = querystring.stringify({
            url,
            comment
        });
        return this._hatenaRequest().post("http://api.b.hatena.ne.jp/1/my/bookmark?" + query);
    }
}

@@ -899,32 +899,33 @@
       .get(%22http
+s
 ://api.b.hatena.
@@ -1317,24 +1317,25 @@
   .get(%22http
+s
 ://api.b.hat
@@ -2096,16 +2096,17 @@
 st(%22http
+s
 ://api.b

use https