var _a;
import { __assign, __decorate, __extends } from "tslib";
import ClipboardJs from 'clipboard';
import { Component, Prop } from 'vue-property-decorator';
import { InputManagement } from '../../mixins/input-management/input-management';
import { InputState } from '../../mixins/input-state/input-state';
import { InputMaxWidth } from '../../mixins/input-width/input-width';
import ToastServicePlugin from '../../utils/toast/toast-service.plugin';
import { ModulVue } from '../../utils/vue/vue';
import { COPY_TO_CLIPBOARD_FEEDBACK_NAME, COPY_TO_CLIPBOARD_NAME, TEXTFIELD_NAME } from '../component-names';
import { MTextfield } from '../textfield/textfield';
import { MCopyToClipboardFeedback } from './copy-to-clipboard-feedback';
import WithRender from './copy-to-clipboard.html';
import './copy-to-clipboard.scss';
var DefaultCopyToClipboardPropsValue = /** @class */ (function () {
    function DefaultCopyToClipboardPropsValue(selection, value) {
        if (selection === void 0) { selection = ''; }
        if (value === void 0) { value = ''; }
        this.selection = selection;
        this.value = value;
    }
    return DefaultCopyToClipboardPropsValue;
}());
function copyToClipboard(text) {
    return new Promise(function (resolve, reject) {
        var fakeElement = document.createElement('button');
        var clipboard = new ClipboardJs(fakeElement, {
            text: function () { return text; },
            action: function () { return 'copy'; },
            container: document.body
        });
        clipboard.on('success', function (result) {
            clipboard.destroy();
            resolve(result);
        });
        clipboard.on('error', function (result) {
            clipboard.destroy();
            reject(result);
        });
        fakeElement.click();
    });
}
var MCopyToClipboard = /** @class */ (function (_super) {
    __extends(MCopyToClipboard, _super);
    function MCopyToClipboard() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.fieldMaxWidth = InputMaxWidth.None;
        _this.labelCopyBtn = _this.$i18n.translate('m-copy-to-clipboard:copy');
        _this.selectedText = '';
        return _this;
    }
    Object.defineProperty(MCopyToClipboard.prototype, "inputProps", {
        get: function () {
            return __assign(__assign({}, this.$props), new DefaultCopyToClipboardPropsValue(this.selectedText, this.value));
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MCopyToClipboard.prototype, "showButton", {
        get: function () {
            return !(!!this.as().disabled || !!this.as().waiting);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MCopyToClipboard.prototype, "buttonHandlers", {
        get: function () {
            return {
                click: this.copyText
            };
        },
        enumerable: false,
        configurable: true
    });
    MCopyToClipboard.prototype.selectText = function () {
        var _this = this;
        requestAnimationFrame(function () {
            _this.selectedText = '';
            _this.$nextTick(function () {
                _this.selectedText = _this.value;
            });
        });
    };
    MCopyToClipboard.prototype.copyText = function () {
        copyToClipboard(this.value);
        this.$emit('copy');
    };
    __decorate([
        Prop({
            default: '',
            required: true
        })
    ], MCopyToClipboard.prototype, "value", void 0);
    __decorate([
        Prop({
            default: true
        })
    ], MCopyToClipboard.prototype, "readonly", void 0);
    MCopyToClipboard = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[TEXTFIELD_NAME] = MTextfield,
                _a),
            mixins: [InputManagement, InputState]
        })
    ], MCopyToClipboard);
    return MCopyToClipboard;
}(ModulVue));
export { MCopyToClipboard };
var CopyToClipboardPlugin = {
    install: function (v) {
        v.use(ToastServicePlugin);
        v.component(COPY_TO_CLIPBOARD_NAME, MCopyToClipboard);
        v.component(COPY_TO_CLIPBOARD_FEEDBACK_NAME, MCopyToClipboardFeedback);
    }
};
export default CopyToClipboardPlugin;
//# sourceMappingURL=copy-to-clipboard.js.map