export class downloader {
    #name;
    #content;
    #type;
    #encode;

    constructor() {
        this.#name = "download.json";
        this.#content = "";
        this.#type = "text/plain";
        this.#encode = "utf-8";
    }

    name(target) {
        this.#name = target;
        return this;
    }

    content(target) {
        this.#content = target;
        return this;
    }

    type(target) {
        this.#type = target;
        return this;
    }

    encode(target) {
        this.#encode = target;
        return this;
    }

    get #href() {
        return (
            "data:" + this.#type 
            + ";charset=" + this.#encode + "," 
            + encodeURIComponent(this.#content)
        );
    }

    get #link() {
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = this.#href;
        link.download = this.#name;

        return link;
    }

    process() {
        const link = this.#link;
        const body = document.querySelector("body");

        body.appendChild(link);
        link.click();
        body.removeChild(link);

        return this;
    }
}