(function (paw, _super) {

    /**
     *  settings {
          blob: Blob,
          url: string,
          controls: boolean,
          downloadUrl: string,
          scaling: boolean,
          previewNotAvailableLabel: string
        }
     * 
     * @param {any} $container
     * @param {any} settings
    */
    paw.lang.extend(XlsxComponent, _super);
    function XlsxComponent($container, settings) {
        _super.call(this, $container, settings, 'fa fa-file-excel-o fa-4x');

        this.result = null;
    }

    XlsxComponent.prototype.renderDocument = function () {
        var self = this;
        return xlsxPreview.xlsx2Html(this.blob, {
            output: 'arrayBuffer',
            minimumRows: 50,
            minimumCols: 30,
        }).then(function (r) {
            self.result = r;
        });
    };

    XlsxComponent.prototype.compilePreview = function () {
        if (paw.utils.value.notSet(this.result)) {
            return paw.utils.async.reject();
        }
        try {
            var url = URL.createObjectURL(new Blob([this.result], {
                type: 'text/html'
            }));

            var $obj = $('<object class="paw-xlsx-previewer" type="text/html" data="' + url + '"></object>');
            this.$container.html($obj);

            if (this.settings.scaling) {
                $obj.width(600);
                $obj.height(600);

                var w = $obj.width();
                var h = $obj.height();

                var delta = this.$container.innerHeight() / h;
                if (delta >= 1) {
                    return;
                }
                $obj.css({
                    transform: 'scale(' + delta + ')'
                });

                $obj.css({
                    'margin-left': -w * 0.5 + w * delta * 0.5,
                    'margin-top': -h * 0.5 + h * delta * 0.5
                });
            }
            return paw.utils.async.promise();
        }
        catch (e) {
            console.error('Error creating Blob:', e);
            return paw.utils.async.reject();
        }
    };
    

    paw.com.XlsxComponent = XlsxComponent;
})(paw, paw.com.DocumentViewerComponent);