"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const index_1 = require("@/constants/index");
const Main_1 = require("@/pages/builder/header/Main");
const system_1 = require("@/pages/system");
const MoreItem = styled_components_1.default.div `
  word-break: break-all;
  line-height: 20px;
  cursor: pointer;
  padding: 8px 24px;
  font-size: 12px;
  width: 100%;
  border-bottom: 1px solid #e8e8e8;
  &:hover {
    color: rgb(24, 144, 255);
    background-color: rgb(242, 242, 242);
  }
  &:last-of-type {
    border-bottom: none;
  }
`;
const mapStateToProps = (store) => ({
    appInfo: store.builder.main.application,
    mock: store.builder.main.mock,
    contentId: store.builder.header.contentId,
    pageLocale: store.builder.header.locale,
    pageType: store.builder.header.fileType,
});
const mapDispatchToProps = {};
const Main = (props) => {
    const { appInfo, contentId, mock, pageLocale, pageType } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { builder } = locale.business;
    const { host = [], slug = '' } = appInfo || {};
    // filter host by page locale
    let pageLocaleHost = host && pageLocale ? host.filter((item) => { var _a; return ((_a = item.locales) === null || _a === void 0 ? void 0 : _a.indexOf(pageLocale)) > -1; }) : [];
    if (!pageLocaleHost || pageLocaleHost.length === 0)
        pageLocaleHost = [host[0]];
    let reals = pageLocaleHost &&
        pageLocaleHost
            .map((item) => (item === null || item === void 0 ? void 0 : item.url) ? `${item.url}/${slug}${index_1.PREVIEW_PATH}?appid=${appInfo.id}&pageid=${contentId}` : '')
            .filter((item) => !!item);
    let debugs = pageLocaleHost &&
        pageLocaleHost
            .map((item) => (item === null || item === void 0 ? void 0 : item.url) ? `${item.url}/${slug}${index_1.DEBUG_PATH}?appid=${appInfo.id}&pageid=${contentId}` : '')
            .filter((item) => !!item);
    if (pageLocale) {
        reals = reals.map((item) => `${item}&locale=${pageLocale}`);
        debugs = debugs.map((item) => `${item}&locale=${pageLocale}`);
    }
    let mocks = [];
    const mockModeEnable = (0, react_1.useMemo)(() => mock === null || mock === void 0 ? void 0 : mock.enable, [mock]);
    if (mockModeEnable) {
        mocks = reals.map((item) => `${item}&mock`);
    }
    const handleClick = (url) => {
        window.open(url);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null, pageType === 'page' && (react_1.default.createElement(antd_1.Popover, { placement: "bottomLeft", overlayClassName: "foxpage-builder-header_popover foxpage-builder-header_preview_popover", trigger: ['hover'], style: { width: 300 }, content: react_1.default.createElement(react_1.default.Fragment, null, (appInfo === null || appInfo === void 0 ? void 0 : appInfo.id) ? (react_1.default.createElement(react_1.default.Fragment, null, reals.length > 0 || debugs.length > 0 ? (react_1.default.createElement(react_1.default.Fragment, null,
            reals.map((url) => (react_1.default.createElement(MoreItem, { key: url, onClick: () => handleClick(url) },
                react_1.default.createElement("span", { style: { color: '#48a700' } },
                    "[ ",
                    react_1.default.createElement(icons_1.EyeOutlined, null),
                    " ]"),
                ' ',
                url))),
            mocks.map((mockUrl) => (react_1.default.createElement(MoreItem, { key: mockUrl, onClick: () => handleClick(mockUrl) },
                react_1.default.createElement("span", { style: { color: '#00a8f7' } },
                    "[ ",
                    react_1.default.createElement(icons_1.BugOutlined, null),
                    " ]"),
                ' ',
                mockUrl))),
            debugs.map((debugUrl) => (react_1.default.createElement(MoreItem, { key: debugUrl, onClick: () => handleClick(debugUrl) },
                react_1.default.createElement("span", { style: { color: '#ff6a5b' } },
                    "[ ",
                    react_1.default.createElement(icons_1.ThunderboltOutlined, null),
                    " ]"),
                ' ',
                debugUrl))))) : (react_1.default.createElement(MoreItem, { key: "unknown" },
            react_1.default.createElement(react_router_dom_1.Link, { to: `/applications/${appInfo.id}/settings/application` },
                react_1.default.createElement(antd_1.Empty, { image: antd_1.Empty.PRESENTED_IMAGE_SIMPLE, description: react_1.default.createElement("span", null, builder.previewUrlEmptyTips) })))))) : (react_1.default.createElement(antd_1.Spin, { spinning: true, size: "small" }))) },
        react_1.default.createElement(Main_1.StyledIcon, null,
            react_1.default.createElement(icons_1.EyeOutlined, null),
            react_1.default.createElement(Main_1.IconMsg, null, builder.preview))))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Main);
