"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const main_1 = require("./main");
const puerts_1 = require("puerts");
var FairyEditor = CS.FairyEditor;
const FileUtils_1 = require("./FileUtils");
const App = FairyEditor.App;
class PsdToFgui {
    start() {
        let pluginPath = FairyEditor.App.pluginManager.basePath;
        let filterExtensions = ["psd"];
        let filter = new CS.SFB.ExtensionFilter("psd", ...filterExtensions);
        // let filters: CS.SFB.ExtensionFilter[] = [filter];
        let filters = CS.System.Array.CreateInstance((0, puerts_1.$typeof)(CS.SFB.ExtensionFilter), 1);
        filters.set_Item(0, filter);
        FairyEditor.IOUtil.BrowseForOpen("选择psd文件", pluginPath, filters, this.onSelectedPsd);
    }
    async onSelectedPsd(psdPath) {
        if (!psdPath) {
            App.consoleView.Log(`没有选择psd文件.`);
            return;
        }
        let psd2fguiPluginsPath = main_1.config.psd2fguiPluginsPath;
        if (!CS.System.IO.Directory.Exists(psd2fguiPluginsPath)) {
            psd2fguiPluginsPath = FairyEditor.App.project.basePath + "/psd2fgui";
            if (!CS.System.IO.Directory.Exists(psd2fguiPluginsPath)) {
                App.consoleView.LogWarning(`需要先配置psd2fgui插件. 路径:${psd2fguiPluginsPath}`);
                return;
            }
        }
        App.consoleView.Log(`psd2fgui插件路径:${psd2fguiPluginsPath} psdPath:${psdPath}`);
        let pkgPath = psdPath.replace(".psd", ".fairypackage");
        // let pkgFolder = psdPath.replace(".psd", "-fairypackage");
        // --nopack
        let result = FairyEditor.ProcessUtil.Start(`node convert ${psdPath} --ignore-font`, null, psd2fguiPluginsPath, false);
        if (!result) {
            while (!CS.System.IO.File.Exists(pkgPath)) {
                FairyEditor.App.consoleView.Log(`psd2fgui sleep wait for ${pkgPath}`);
                await FileUtils_1.default.sleep(1000);
            }
            App.consoleView.Log(`psd2fgui completed. 使用菜单<资源->导入资源包> 可以导入该资源包`);
            // App.Alert(`psd2fgui completed. 使用菜单<资源->导入资源包> 可以导入该资源包`);
        }
        else {
            App.consoleView.LogError(`psd2fgui 失败. result:${result}`);
        }
        // App.consoleView.Log(`psdToFgui result:${result} exist:${CS.System.IO.File.Exists(pkgPath)}`);
        // let pkg = FairyEditor.App.project.CreatePackage(pkgPath);
        // App.consoleView.Log(`psdToFgui pkgName:${pkg?.name ?? "null"}`);
        // if (pkg) {
        //     FairyEditor.App.libView.ShowImportResourcesDialog(pkg);
        // }
    }
}
exports.default = PsdToFgui;
