import { __classPrivateFieldGet, __classPrivateFieldSet, __esDecorate, __runInitializers } from "tslib";
import * as React from 'react';
import { PanelButtonWidget, ComboBoxStore, inject } from '@journeyapps-labs/reactor-mod';
import { selectFile, readFileAsArrayBuffer, MimeTypes } from '@journeyapps-labs/lib-reactor-utils';
import * as path from 'path';
import JSZip from 'jszip';
import * as json5 from 'json5';
const SUPPORTED_EXTENSIONS = [
    /**
     * Visual studio extension
     */
    '.vsix'
];
let UploadVSIXThemeBtnWidget = (() => {
    var _a, _UploadVSIXThemeBtnWidget_comboBoxStore_accessor_storage;
    let _classSuper = React.Component;
    let _comboBoxStore_decorators;
    let _comboBoxStore_initializers = [];
    let _comboBoxStore_extraInitializers = [];
    return _a = class UploadVSIXThemeBtnWidget extends _classSuper {
            get comboBoxStore() { return __classPrivateFieldGet(this, _UploadVSIXThemeBtnWidget_comboBoxStore_accessor_storage, "f"); }
            set comboBoxStore(value) { __classPrivateFieldSet(this, _UploadVSIXThemeBtnWidget_comboBoxStore_accessor_storage, value, "f"); }
            constructor(props) {
                super(props);
                _UploadVSIXThemeBtnWidget_comboBoxStore_accessor_storage.set(this, __runInitializers(this, _comboBoxStore_initializers, void 0));
                __runInitializers(this, _comboBoxStore_extraInitializers);
                this.state = {};
            }
            async doUpload(event, file) {
                if (!file) {
                    return;
                }
                // check for supported extensions
                if (SUPPORTED_EXTENSIONS.indexOf(path.extname(file.name)) === -1) {
                    return;
                }
                const data = await readFileAsArrayBuffer(file);
                const new_zip = new JSZip();
                const extraction = await new_zip.loadAsync(data);
                //load package.json
                try {
                    const packageContent = await extraction.files['extension/package.json'].async('text');
                    const content = json5.parse(packageContent);
                    // extension might now have themes
                    if (!content.contributes.themes || content.contributes.themes.length === 0) {
                        return;
                    }
                    let selected = content.contributes.themes[0].path;
                    if (content.contributes.themes.length > 1) {
                        const userSelected = await this.comboBoxStore.showComboBox(content.contributes.themes.map((theme) => {
                            return {
                                key: theme.path,
                                title: theme.label
                            };
                        }), event);
                        if (!userSelected) {
                            return;
                        }
                        selected = userSelected.key;
                    }
                    const themeFile = path.join('extension', selected);
                    const themeContent = await extraction.files[themeFile].async('text');
                    const themeObject = json5.parse(themeContent);
                    this.props.gotTheme(themeObject);
                }
                catch (ex) {
                    console.error(ex);
                    return;
                }
            }
            render() {
                return (React.createElement(PanelButtonWidget, { action: async (event, loading) => {
                        const file = await selectFile({
                            mimeTypes: [MimeTypes.VSIX]
                        });
                        loading(true);
                        await this.doUpload(event, file);
                        loading(false);
                    }, label: "Upload VSCode theme" }));
            }
        },
        _UploadVSIXThemeBtnWidget_comboBoxStore_accessor_storage = new WeakMap(),
        (() => {
            var _b;
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create((_b = _classSuper[Symbol.metadata]) !== null && _b !== void 0 ? _b : null) : void 0;
            _comboBoxStore_decorators = [inject(ComboBoxStore)];
            __esDecorate(_a, null, _comboBoxStore_decorators, { kind: "accessor", name: "comboBoxStore", static: false, private: false, access: { has: obj => "comboBoxStore" in obj, get: obj => obj.comboBoxStore, set: (obj, value) => { obj.comboBoxStore = value; } }, metadata: _metadata }, _comboBoxStore_initializers, _comboBoxStore_extraInitializers);
            if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        })(),
        _a;
})();
export { UploadVSIXThemeBtnWidget };
//# sourceMappingURL=UploadVSIXThemeBtnWidget.js.map