"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const prefix_classname_1 = __importDefault(require("prefix-classname"));
const react_dnd_1 = require("react-dnd");
const const_1 = require("../../../config/const");
const externals_modules_1 = require("../../utils/externals-modules");
const use_shared_1 = require("@rcp/use.shared");
const cn = (0, prefix_classname_1.default)('');
const c = (0, prefix_classname_1.default)(`${const_1.CLS_PREFIX}-stage`);
require("./style.scss");
const header_1 = require("../header");
const pipe_1 = require("../../../../shared/pipe");
const location_widget_1 = __importDefault(require("../location-widget"));
(0, pipe_1.setId)(`main`);
const StageContent = react_1.default.memo(({ className, externalModules, bundlerURL }) => {
    react_1.default.useLayoutEffect(() => !!externalModules && (0, externals_modules_1.addModules)(externalModules), [externalModules]);
    const iframeRef = react_1.default.useRef(null);
    react_1.default.useEffect(() => {
        var _a;
        if ((_a = iframeRef.current) === null || _a === void 0 ? void 0 : _a.contentWindow) {
            (0, pipe_1.addRemoteGlobalThis)(iframeRef.current.contentWindow);
            return () => {
                var _a;
                (0, pipe_1.removeRemoteGlobalThis)((_a = iframeRef.current) === null || _a === void 0 ? void 0 : _a.contentWindow);
            };
        }
    }, [iframeRef]);
    const [action, setAction] = (0, const_1.useLocationAction)();
    react_1.default.useLayoutEffect(() => {
        setAction({ action: 'PUSH', url: bundlerURL, outer: true });
    }, [bundlerURL]);
    const [iframeSrc, setIframeSrc] = react_1.default.useState(bundlerURL);
    react_1.default.useLayoutEffect(() => {
        if (!action) {
            return;
        }
        if (action.url.includes('#')) {
            setIframeSrc(action.url);
            return;
        }
        if (action.outer) {
            setIframeSrc(action.url);
        }
    }, [action]);
    const [{ showLocation }] = (0, header_1.useHeaderStatus)();
    const [api] = (0, use_shared_1.useShared)('api');
    const ddManager = (0, react_dnd_1.useDragDropManager)();
    const _sharedMap = (0, use_shared_1.useSharedMap)();
    const _sharedUpdateMap = (0, use_shared_1.useSharedUpdateMap)();
    react_1.default.useLayoutEffect(() => (0, pipe_1.register)('shared', {
        api,
        overingNodeSubject: const_1.overingNodeSubject,
        selectedNodeSubject: const_1.selectedNodeSubject,
        headerStatusSubject: header_1.headerStatusSubject,
        locationActionSubject: const_1.locationActionSubject,
        ddManager,
        _sharedMap,
        _sharedUpdateMap
    }), [ddManager, _sharedMap, _sharedUpdateMap, api]);
    return (react_1.default.createElement("div", { className: cn(c(), className) },
        !!showLocation && react_1.default.createElement(location_widget_1.default, { iframeRef: iframeRef }),
        react_1.default.createElement("iframe", { ref: iframeRef, src: iframeSrc, className: c('__iframe') })));
});
const Stage = (props) => {
    return react_1.default.createElement(StageContent, Object.assign({}, props));
};
Stage.defaultProps = {
    bundlerURL: '/bundler.html'
};
exports.default = Stage;
//# sourceMappingURL=index.js.map