import { __decorate, __metadata } from "tslib";
import { Autowired } from '@opensumi/di';
import { AppConfig, Domain, path as paths, URI } from '@opensumi/ide-core-browser';
import { StaticResourceContribution as CoreStaticResourceContribution, } from '@opensumi/ide-core-browser/lib/static-resource';
import { IWorkspaceService } from '@opensumi/ide-workspace';
import { IPropsService } from '../../props.service';
const EXPRESS_SERVER_PATH = window.location.href;
// file 文件资源 远程读取
let EditorStaticResourceContribution = class EditorStaticResourceContribution {
    registerStaticResolver(service) {
        // 用来打开 raw 文件，如 jpg
        service.registerStaticResourceProvider({
            scheme: 'file',
            resolveStaticResource: (uri) => {
                var _a;
                const documentModel = this.propsService.props.documentModel;
                if (!(this.appConfig.staticServicePath
                    || (documentModel && documentModel.resolveStaticResourcePath))) {
                    return uri;
                }
                // 将 file 协议转为代码托管平台提供的 raw 服务
                const assetsUri = new URI(this.appConfig.staticServicePath);
                const rootUri = new URI((_a = this.workspaceService.workspace) === null || _a === void 0 ? void 0 : _a.uri);
                const relativePath = rootUri.relative(uri);
                if (!relativePath) {
                    return uri;
                }
                // FIXME: 由于 resolveStaticResource 是同步方法，这里 hardcode 了 workspaceManagerService 的实现
                let relativePathStr = relativePath.toString();
                // trim leading whitespace
                if (relativePathStr.startsWith(paths.Path.separator)) {
                    relativePathStr = relativePathStr.slice(1);
                }
                if (documentModel && documentModel.resolveStaticResourcePath) {
                    return documentModel.resolveStaticResourcePath(documentModel);
                }
                return assetsUri.withPath(assetsUri.path.join(relativePathStr));
            },
            roots: [this.appConfig.staticServicePath || EXPRESS_SERVER_PATH],
        });
    }
};
__decorate([
    Autowired(AppConfig),
    __metadata("design:type", Object)
], EditorStaticResourceContribution.prototype, "appConfig", void 0);
__decorate([
    Autowired(IWorkspaceService),
    __metadata("design:type", Object)
], EditorStaticResourceContribution.prototype, "workspaceService", void 0);
__decorate([
    Autowired(IPropsService),
    __metadata("design:type", Object)
], EditorStaticResourceContribution.prototype, "propsService", void 0);
EditorStaticResourceContribution = __decorate([
    Domain(CoreStaticResourceContribution)
], EditorStaticResourceContribution);
export { EditorStaticResourceContribution };
//# sourceMappingURL=static-resource.contribution.js.map