import { enumToEnumOptionMapper, getI18nKeyPrefix, getTranslator, mapStateToControlProps, rankWith, } from '@jsonforms/core';
import { html, repeat } from '@microsoft/fast-element';
import { isOneOfEnum } from '../testers/isOneOfEnum';
import { ENUM_CONTROL_RANK } from './RenderersRanks';
// overrides https://github.com/eclipsesource/jsonforms/blob/69441cdfc48f359850d87fa4b842856f1f139eb2/packages/core/src/util/renderer.ts#L515
// to allow mapping of enums that come as oneOf
export const mapStateToEnumControlProps = (state, ownProps) => {
    const props = mapStateToControlProps(state, ownProps);
    const options = ownProps.options ||
        (props.schema.enum || props.schema.oneOf.find((x) => x.hasOwnProperty('enum')).enum).map((e) => enumToEnumOptionMapper(e, getTranslator()(state), getI18nKeyPrefix(props.schema, props.uischema, props.path))) ||
        (props.schema.const && [
            enumToEnumOptionMapper(props.schema.const, getTranslator()(state), getI18nKeyPrefix(props.schema, props.uischema, props.path)),
        ]);
    return Object.assign(Object.assign({}, props), { options });
};
export const EnumControlRendererTemplate = html `
  <template>
    <control-wrapper
      :control=${(x) => x.control}
      :touched=${(x) => x.touched}
      ?submitted=${(x) => x.submitted}
    >
      <zero-combobox
        style="width: 100%"
        ?disabled=${(x) => !x.control.enabled}
        :value=${(x) => { var _a; return ((_a = x.control.options.find((option) => option.value === x.control.data)) === null || _a === void 0 ? void 0 : _a.label) || ''; }}
        @change=${(x, c) => {
    var _a;
    x.control.handleChange(x.control.path, (_a = x.control.options.find((option) => option.label === c.event.target.value)) === null || _a === void 0 ? void 0 : _a.value);
    x.onBlur();
}}
      >
        ${repeat((x) => x.control.options, html `
            <zero-option>${(x) => x.label}</zero-option>
          `)}
      </zero-combobox>
    </control-wrapper>
  </template>
`;
export const EnumControlRendererEntry = {
    renderer: EnumControlRendererTemplate,
    tester: rankWith(ENUM_CONTROL_RANK, isOneOfEnum),
    mapper: mapStateToEnumControlProps,
};
//# sourceMappingURL=EnumControlRenderer.js.map