"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkAuth = exports.getStaticResourceAttribute = exports.isScopeSlot = exports.isSlot = exports.resolveComponentProps = exports.isPlainObject = exports.isEmptyObj = exports.camelcase = exports.kebabCase = exports.bindFuncObj = exports.resolveDataBinds = void 0;
var tslib_1 = require("tslib");
var widgets_1 = require("./widgets");
var adapter_1 = require("./adapter");
/**
 * All data bindings are generated as functions: (forItems) => any
 * @param {*} dataBinds
 * @param {*} forItems
 */
function resolveDataBinds(dataBinds, forItems, codeContext, scopeContext, throwError, dataContext) {
    var resolvedProps = {};
    for (var prop in dataBinds) {
        var fn = dataBinds[prop];
        try {
            if (codeContext && codeContext.$WEAPPS_COMP) {
                fn = fn.bind(codeContext.$WEAPPS_COMP);
            }
            resolvedProps[prop] = fn(forItems, codeContext && codeContext.event, dataContext);
        }
        catch (e) {
            console.error('Error resolving data binding', prop, dataBinds[prop], e);
            if (throwError) {
                throw e;
            }
        }
    }
    return resolvedProps;
}
exports.resolveDataBinds = resolveDataBinds;
/**
 * 对函数进行批量绑定
 * @param {*} funcObj
 * @param {*} context
 */
function bindFuncObj(funcObj, context) {
    if (funcObj === void 0) { funcObj = {}; }
    return Object.keys(funcObj).reduce(function (obj, fnName) {
        var _a, _b;
        obj[fnName] = (_b = (_a = funcObj[fnName]) === null || _a === void 0 ? void 0 : _a.bind) === null || _b === void 0 ? void 0 : _b.call(_a, context);
        return obj;
    }, {});
}
exports.bindFuncObj = bindFuncObj;
function kebabCase(str) {
    return str.replace(/[A-Z]/g, function (match) {
        return '-' + match.toLowerCase();
    });
}
exports.kebabCase = kebabCase;
function camelcase(str, firstUpperCase) {
    if (firstUpperCase === void 0) { firstUpperCase = false; }
    str = str.replace(/[_-]([a-z])/g, function (l) {
        return l[1].toUpperCase();
    });
    if (firstUpperCase)
        str = str.charAt(0).toUpperCase() + str.slice(1);
    return str;
}
exports.camelcase = camelcase;
var isEmptyObj = function (obj) {
    if (!isPlainObject(obj)) {
        return false;
    }
    for (var i in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, i)) {
            return false;
        }
    }
    return true;
};
exports.isEmptyObj = isEmptyObj;
function isPlainObject(src) {
    return Object.prototype.toString.call(src) === '[object Object]';
}
exports.isPlainObject = isPlainObject;
/**
 * 用于处理自定义组件props传参结构，对系统变量进行保留
 */
function resolveComponentProps(props, isPlainProps) {
    var _a;
    var staticResourceAttribute = props.staticResourceAttribute;
    staticResourceAttribute &&
        staticResourceAttribute.map(function (property) {
            if (props.data && props.data[property]) {
                props.data[property] = getStaticResourceAttribute(props.data[property]);
            }
        });
    if (!isPlainProps) {
        return (0, tslib_1.__assign)({}, props);
    }
    var _b = props.data, data = _b === void 0 ? {} : _b, _c = props.events, events = _c === void 0 ? [] : _c, restProps = (0, tslib_1.__rest)(props, ["data", "events"]);
    var customProps = (0, tslib_1.__assign)({}, data);
    var builtinProps = (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([
        // react 保留字
        'ref',
        'key',
        'dangerouslySetInnerHTML',
        'className',
        'htmlFor',
        'style',
        'contentEditable',
        // lowcode 保留字
        'events',
        'children',
        '_parentId',
        '_visible',
        '_listeners'
    ], (0, tslib_1.__read)(widgets_1.widgetKeys), false), [
        'classList',
        'widgetType',
        'staticResourceAttribute',
        // 小程序保留字
        'id',
        'class',
        'hidden',
        'slot',
    ], false);
    // delete builtin props
    builtinProps.map(function (prop) { return delete customProps[prop]; });
    // 选区events处理
    var _selectableBlockEvents = customProps._selectableBlockEvents, restData = (0, tslib_1.__rest)(customProps, ["_selectableBlockEvents"]);
    var _selectableBlockEmit = _selectableBlockEvents.emit;
    var selectableBlockEvents = ((_a = _selectableBlockEvents === null || _selectableBlockEvents === void 0 ? void 0 : _selectableBlockEvents.events) === null || _a === void 0 ? void 0 : _a.reduce(function (events, item) {
        var propName = item;
        events[propName] = function (e) { return _selectableBlockEmit(propName, e); };
        return events;
    }, {})) || {};
    try {
        _selectableBlockEvents.events = new Proxy(selectableBlockEvents, {
            get: function (obj, prop) {
                return prop in obj ? obj[prop] : function (e) { return _selectableBlockEmit(prop, e); };
            },
        });
    }
    catch (e) {
        console.warn('selectableBlockEvents', selectableBlockEvents);
    }
    return (0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, restData), restProps), { _selectableBlockEvents: _selectableBlockEvents, events: new Proxy(events.reduce(function (events, item) {
            var propName = item;
            events[propName] = function (e) { return restProps.emit(propName, e); };
            return events;
        }, {}), {
            get: function (obj, prop) {
                return prop in obj ? obj[prop] : function (e) { return restProps.emit(prop, e); };
            },
        }) });
}
exports.resolveComponentProps = resolveComponentProps;
var SCOPE_SLOT_COMPONENT_LIB = ['tea_basis', 'tea_shop', 'crm_basis'];
var SCOPE_SLOT_MAP = SCOPE_SLOT_COMPONENT_LIB.reduce(function (map, lib) {
    map[lib + ":TableMatching"] = {
        headerSlot: true,
        recordSlot: true,
    };
    map[lib + ":TableExpanded"] = {
        headerSlot: true,
        recordSlot: true,
    };
    map[lib + ":TabsTable"] = {
        headerSlot: true,
        recordSlot: true,
    };
    map[lib + ":Table"] = {
        headerSlot: true,
        recordSlot: true,
    };
    return map;
}, {});
function isSlot(comp) {
    return !comp['module'] && !comp.component;
}
exports.isSlot = isSlot;
function isScopeSlot(comp, slot) {
    var sourceKey = comp.module && comp.component && comp.module + ":" + comp.component;
    var map = SCOPE_SLOT_MAP[sourceKey];
    return map && map[slot];
}
exports.isScopeSlot = isScopeSlot;
function getStaticResourceAttribute(staticUrl) {
    if (/^\//.test(staticUrl)) {
        var _a = (adapter_1.runtime['app'] || {}).domain, domain = _a === void 0 ? '' : _a;
        var url = "https://" + domain + staticUrl;
        return url;
    }
    return staticUrl;
}
exports.getStaticResourceAttribute = getStaticResourceAttribute;
/**
 * 检查页面权限
 **/
function checkAuth(app, appId, pageId) {
    var _a, _b;
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
        var checkAuthResult, isLogin;
        return (0, tslib_1.__generator)(this, function (_c) {
            switch (_c.label) {
                case 0: return [2 /*return*/, true];
                case 1:
                    checkAuthResult = _c.sent();
                    isLogin = false;
                    if (Array.isArray(checkAuthResult) && checkAuthResult.length > 0) {
                        isLogin = (_b = (_a = checkAuthResult[0]) === null || _a === void 0 ? void 0 : _a.IsAccess) !== null && _b !== void 0 ? _b : false;
                    }
                    app.hideNavigationBarLoading();
                    if (!isLogin) {
                        app.showToast({
                            title: '页面无访问权限',
                            icon: 'error',
                        });
                    }
                    return [2 /*return*/, isLogin];
            }
        });
    });
}
exports.checkAuth = checkAuth;
//# sourceMappingURL=common.js.map