"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 antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/applications/detail/file/variables"));
const events_1 = require("@/actions/builder/events");
const PAGE_ACTIONS = __importStar(require("@/actions/builder/main"));
const index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const components_1 = require("./components");
const PAGE_NUM = 1;
const PAGE_SIZE = 999;
const Modal = (0, styled_components_1.default)(antd_1.Modal) `
  height: 60%;
  .ant-modal-content {
    height: 100%;
    .ant-modal-body {
      height: calc(100% - 110px);
      overflow-y: auto;
    }
  }
`;
const Content = styled_components_1.default.div `
  display: flex;
  height: 100%;
  > div > div + div {
    height: calc(100% - 22px);
  }
`;
const VariableContent = styled_components_1.default.div `
  flex: 1;
  margin-right: 12px;
  .variable-list {
    border: 1px solid #1f38584d;
    overflow-y: auto;
    > ul {
      list-style: none;
      line-height: 36px;
      padding: 0;
      > li {
        padding: 0 18px;
        cursor: pointer;
        &:hover {
          background: #1f38580a;
        }
      }
    }
  }
`;
const BindContent = styled_components_1.default.div `
  flex: 2;
  .variable-bind-content {
    > textarea {
      transition: all 0.3s;
      border-radius: 2px;
      &:focus-visible {
        outline: 0;
        border-color: #40a9ff;
        box-shadow: 0 0 0 1px #40a9ff;
      }
    }
  }
`;
const mapStateToProps = (state) => ({
    variables: state.applications.detail.file.variables.list,
    loading: state.applications.detail.file.variables.loading,
    applicationId: state.builder.header.applicationId,
    folderId: state.builder.header.folderId,
    data: state.builder.main.toolbarModalData,
    type: state.builder.main.toolbarModalType,
    visible: state.builder.main.toolbarModalVisible,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    fetchVariableList: ACTIONS.fetchList,
    closeModal: PAGE_ACTIONS.updateToolbarModalVisible,
    variableBind: events_1.variableBind,
};
const VariableBind = (props) => {
    const { loading, folderId, applicationId, data, type, variables, visible, clearAll, closeModal, fetchVariableList, variableBind, } = props;
    const [value, setValue] = (0, react_1.useState)('');
    const variableBindRef = (0, react_1.useRef)(null);
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, variable } = locale.business;
    const open = (0, react_1.useMemo)(() => visible && type === 'variableBind', [visible, type]);
    const component = (0, react_1.useMemo)(() => {
        const { component } = data || {};
        return component;
    }, [data]);
    const keys = (0, react_1.useMemo)(() => {
        const { keys } = data || {};
        return keys;
    }, [data]);
    (0, react_1.useEffect)(() => {
        var _a;
        if (open) {
            if (applicationId && folderId) {
                fetchVariableList({ applicationId, folderId, page: PAGE_NUM, size: PAGE_SIZE });
            }
            const _value = ((_a = component === null || component === void 0 ? void 0 : component.props) === null || _a === void 0 ? void 0 : _a[keys]) || '';
            setValue(_value);
        }
        else {
            clearAll();
        }
    }, [open, applicationId, folderId]);
    const handleClick = (str) => {
        var _a;
        (_a = variableBindRef.current) === null || _a === void 0 ? void 0 : _a.replaceProps(str);
    };
    const handleCancel = () => {
        closeModal(false);
    };
    const handleOk = () => {
        variableBind(keys, value);
        handleCancel();
    };
    const editorContentByType = (0, react_1.useMemo)(() => {
        switch (type) {
            case index_2.EditorInputEnum.HtmlString:
                return react_1.default.createElement(components_1.HtmlBind, { ref: variableBindRef });
            case index_2.EditorInputEnum.Object:
                return react_1.default.createElement(components_1.JSONBind, { ref: variableBindRef });
            default:
                return react_1.default.createElement(components_1.TextBind, { ref: variableBindRef });
        }
    }, [type, value]);
    return (react_1.default.createElement(Modal, { destroyOnClose: true, maskClosable: false, width: "60%", title: variable.bind, visible: open, onCancel: handleCancel, onOk: handleOk },
        react_1.default.createElement(Content, null,
            react_1.default.createElement(VariableContent, null,
                react_1.default.createElement(index_1.Title, null, global.variables),
                react_1.default.createElement("div", { className: "variable-list" },
                    loading && react_1.default.createElement(antd_1.Spin, { spinning: true }),
                    react_1.default.createElement("ul", null, variables === null || variables === void 0 ? void 0 : variables.map((variable) => {
                        return (react_1.default.createElement("li", { key: variable.id, onClick: () => {
                                handleClick(`{{${variable.name}}}`);
                            } }, variable.name));
                    })))),
            react_1.default.createElement(BindContent, null,
                react_1.default.createElement(index_1.Title, null, variable.content),
                react_1.default.createElement("div", { className: "variable-bind-content" },
                    react_1.default.createElement(components_1.BindContext.Provider, { value: { value: value || '', setValue } }, editorContentByType),
                    react_1.default.createElement("div", { style: { height: '50%' } },
                        react_1.default.createElement(index_1.Title, null, variable.useVariableTitle),
                        react_1.default.createElement("ul", { style: { paddingLeft: 16 } },
                            react_1.default.createElement("li", null,
                                variable.useVariableTip,
                                '"{{variableA}}"',
                                ")"),
                            react_1.default.createElement("li", null,
                                variable.useVariableAttrTip,
                                '"{{variableB:name}}"',
                                ")"))))))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(VariableBind);
