"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.imports = void 0;
const transpile_1 = require("../../transpile");
const utils_1 = require("../utils");
exports.imports = [
    { name: 'WmChips', from: '@wavemaker/app-rn-runtime/components/input/chips/chips.component' }
];
const createChipsArrayExpression = (element, attrName) => {
    let expression = element.attrs[attrName];
    if (expression.includes('[0]')) {
        const bindExpr = expression.substring(5);
        const transformedExpression = bindExpr.replace(/\[0\](?=[^[]*$)/, '[index]');
        return `{(index) => {
      try {
        const result = fragment.eval(() => ${transformedExpression});
        return result;
      } catch(e) {
        return null;
      }
    }}`;
    }
    // Handle bind expressions without [0] - need to remove bind: prefix
    if (!expression.includes('[0]') && expression) {
        const bindExpression = expression.substring(5);
        return `{(index) => fragment.eval(() => ${bindExpression})}`;
    }
    return `{(index) => ${expression}}`;
};
const handleAttribute = (element, attrName, propName) => {
    const attrValue = element.getAttribute(attrName);
    if (attrValue && attrValue.includes('bind:')) {
        const arrayExpression = createChipsArrayExpression(element, attrName);
        if (arrayExpression) {
            element.removeAttribute(attrName);
            return `${propName}=${arrayExpression}`; // Will be a function! 
        }
    }
    // Handle empty string case - don't include the prop at all
    if (!attrValue || attrValue.trim() === '') {
        return '';
    }
    return `${propName}={() => "${attrValue}"}`;
};
exports.default = {
    pre: (element, context) => {
        let getDisplayExpression = (0, transpile_1.createExpression)(element, 'displayexpression');
        if (getDisplayExpression) {
            element.removeAttribute('displayexpression');
        }
        const getLeftIconClassName = handleAttribute(element, 'lefticonclass', 'getLeftIconClassName');
        const getRightIconClassName = handleAttribute(element, 'righticonclass', 'getRightIconClassName');
        const getLeftBadge = handleAttribute(element, 'leftbadge', 'getLeftBadge');
        const getRightBadge = handleAttribute(element, 'rightbadge', 'getRightBadge');
        const onChange = (0, utils_1.getOnChangeExprforFormWidget)(element, 'on-change');
        return `<WmChips ${(0, transpile_1.transformAttrs)(element, context)} ${getDisplayExpression ? `getDisplayExpression=${getDisplayExpression}` : ''} ${getLeftIconClassName} ${getRightIconClassName} ${getLeftBadge} ${getRightBadge} ${onChange ? `onChange={${onChange}}` : ''}>`;
    },
    post: (element, context) => '</WmChips>',
    imports: (element, context) => exports.imports
};
//# sourceMappingURL=chips.transformer.js.map