'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-0b92fb83.js');

class UploadAdapter {
    constructor({ loader, serviceUpload }) {
        this.loader = loader;
        this.serviceUpload = serviceUpload;
    }
    async upload() {
        const file = await this.loader.file;
        try {
            const imageUrl = await this.serviceUpload(file);
            return {
                default: imageUrl
            };
        }
        catch (e) {
            throw new Error(e);
        }
    }
}

const CcTextarea = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.color = "primary";
        this.error = false;
        this.success = false;
        this.disabled = false;
        this.rich = false;
        this.focusEditor = () => {
            if (this.rich) {
                this.editorInstance.editing.view.focus();
            }
            else {
                this.textAreaEl.focus();
            }
        };
        this.changeText = core.createEvent(this, "changeText", 7);
    }
    validateName(newDisabled) {
        if (this.editorInstance)
            this.editorInstance.isReadOnly = newDisabled;
    }
    toggleRichText(richNew, richOld) {
        if (richNew !== richOld) {
            if (richNew && !richNew) {
                this.enableRichTextEditor();
            }
            else {
                this.disableRichTextEditor();
            }
        }
    }
    setValue(newValue) {
        if (this.rich) {
            if (this.editorInstance)
                this.editorInstance.data.set(newValue);
        }
        else {
            this.textAreaEl.value = newValue;
        }
    }
    changeTextHandler(newText) {
        this.changeText.emit(newText);
    }
    setRichTextEditorDefaults() {
        this.editorInstance.isReadOnly = this.disabled;
        if (this.value)
            this.editorInstance.data.set(this.value);
        this.editorInstance.model.document.on("change:data", () => {
            this.changeTextHandler(this.editorInstance.getData());
        });
    }
    async enableRichTextEditor() {
        let toolbar = [
            "Undo",
            "Redo",
            "Heading",
            "Bold",
            "Italic",
            "numberedList",
            "bulletedList",
            "Link",
            "blockQuote"
        ];
        if (!this.rich)
            return null;
        const { default: ClassicEditor } = await new Promise(function (resolve) { resolve(require('./ckeditor-a23e20d2.js')); });
        if (this.enableImage && this.imageService) {
            toolbar.push("imageUpload");
        }
        this.editorInstance = await ClassicEditor.create(this.richTextEl, {
            toolbar,
            placeholder: this.placeholder
        });
        this.setRichTextEditorDefaults();
        this.setAdapterUpload(this.editorInstance);
        return this.editorInstance;
    }
    setAdapterUpload(editor) {
        if (!this.enableImage && !this.imageService) {
            return;
        }
        editor.plugins.get("FileRepository").createUploadAdapter = loader => {
            return new UploadAdapter({ loader, serviceUpload: this.imageService });
        };
    }
    disableRichTextEditor() {
        if (this.rich && this.editorInstance)
            this.editorInstance.destroy();
    }
    componentDidLoad() {
        this.enableRichTextEditor();
    }
    componentDidUnload() {
        this.disableRichTextEditor();
    }
    render() {
        return (core.h(core.Host, { class: {
                textarea: true,
                "textarea--primary": this.color === "primary",
                "textarea--secondary": this.color === "secondary",
                "textarea--success": this.success && !this.error && !this.disabled,
                "textarea--error": this.error && !this.success && !this.disabled,
                "textarea--disabled": this.disabled
            }, "data-testid": "cc-textarea" }, this.label && (core.h("label", { class: "textarea__label", onClick: this.focusEditor }, this.label)), this.rich ? (core.h("div", { class: "textarea__richTextSkeleton", ref: el => (this.richTextEl = el) }, this.value ? this.value : "")) : (core.h("textarea", { ref: el => (this.textAreaEl = el), disabled: this.disabled, placeholder: this.placeholder, class: "textarea__field", name: this.name, value: this.value, onInput: e => this.changeTextHandler(e.target.value) })), this.helperText && this.error && !this.success && !this.disabled && (core.h("span", { class: "textarea__helperText", onClick: this.focusEditor }, this.helperText))));
    }
    static get watchers() { return {
        "disabled": ["validateName"],
        "rich": ["toggleRichText"],
        "value": ["setValue"]
    }; }
    static get style() { return ".textarea{display:block}.textarea h2,.textarea h3,.textarea h4{font-weight:900}.textarea h2{font-size:2rem}.textarea h3{font-size:1.8rem}.textarea h4{font-size:1.6rem}.textarea li{margin-left:1.6rem}.textarea ol{list-style:decimal}.textarea ul{list-style:disc}.textarea b,.textarea strong{font-weight:900}.textarea a{text-decoration:underline}.textarea.textarea--primary a{color:var(--primary)}.textarea.textarea--secondary a{color:var(--secondary)}.textarea__field{width:100%;border-radius:.4rem;border-width:1px;border-color:var(--neutral-02);padding-left:1.6rem;padding-right:1.6rem;padding-top:.8rem;padding-bottom:.8rem;background-color:var(--neutral-04);font-size:1.4rem;display:block;line-height:1.625;resize:none;min-height:13.6rem}.textarea__field:disabled{border-color:var(--disabled-background)}.textarea--success .textarea__field{border-color:var(--success)}.textarea--error .textarea__field{border-color:var(--error)}.textarea__field:focus{outline:0}.textarea--primary .textarea__field:focus{border-color:var(--primary)}.textarea--secondary .textarea__field:focus{border-color:var(--secondary)}.textarea--success .textarea__field:focus{border-color:var(--success)}.textarea--error .textarea__field:focus{border-color:var(--error)}.textarea__label{display:block;padding-top:.4rem;padding-bottom:.4rem;font-size:1.1rem;font-weight:900;color:var(--neutral-03)}.textarea--disabled .textarea__label{color:var(--disabled-text)}.textarea__helperText{display:block;font-size:1.1rem;margin-top:.4rem;color:var(--error)}.textarea__richTextSkeleton{width:100%;border-radius:.4rem;border-width:1px;border-color:var(--neutral-02);padding-left:1.6rem;padding-right:1.6rem;padding-top:.8rem;padding-bottom:.8rem;background-color:var(--neutral-04);font-size:1.4rem;display:block;line-height:1.625;min-height:13.6rem}.textarea__richTextSkeleton+.textarea__helperText{display:none}.textarea .ck.ck-toolbar{border-color:var(--neutral-02)}.textarea.textarea--disabled .ck.ck-toolbar{border-color:var(--disabled-background)}.textarea .ck-rounded-corners .ck.ck-editor__top .ck-sticky-panel .ck-toolbar,.textarea .ck.ck-editor__top .ck-sticky-panel .ck-toolbar.ck-rounded-corners{border-top-left-radius:.4rem;border-top-right-radius:.4rem}.textarea .ck-rounded-corners .ck.ck-editor__main>.ck-editor__editable,.textarea .ck.ck-editor__main>.ck-editor__editable.ck-rounded-corners{border-bottom-right-radius:.4rem;border-bottom-left-radius:.4rem}.textarea .ck.ck-editor__main>.ck-editor__editable.ck-focused{-webkit-box-shadow:none;box-shadow:none}.textarea .ck.ck-editor__main>.ck-editor__editable:not(.ck-focused){border-color:var(--neutral-02)}.textarea .ck.ck-editor__main>.ck-editor__editable:not(.ck-focused).ck-read-only{border-color:var(--disabled-background)}.textarea .ck.ck-toolbar.ck-toolbar_grouping>.ck-toolbar__items{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end}.textarea.textarea--primary .ck.ck-editor__main>.ck-editor__editable.ck-focused{border-color:var(--primary)}.textarea.textarea--secondary .ck.ck-editor__main>.ck-editor__editable.ck-focused{border-color:var(--secondary)}.textarea--error .textarea{border-color:var(--error)}.textarea.textarea--success .ck.ck-editor__main>.ck-editor__editable.ck-focused,.textarea.textarea--success .ck.ck-editor__main>.ck-editor__editable:not(.ck-focused){border-color:var(--success)}.textarea.textarea--error .ck.ck-editor__main>.ck-editor__editable.ck-focused,.textarea.textarea--error .ck.ck-editor__main>.ck-editor__editable:not(.ck-focused){border-color:var(--error)}.textarea .ck.ck-editor__editable_inline>:first-child{margin-top:.8rem}.textarea .ck.ck-editor__editable_inline>:last-child{margin-bottom:.8rem}.textarea .ck.ck-editor__editable_inline{padding-left:1.6rem;padding-right:1.6rem;min-height:13.6rem}"; }
};

exports.cc_textarea = CcTextarea;
