from abstra_internals.widgets.widget_base import Input


class CodeInput(Input):
    type = 'code-input'
    empty_value = ''

    def __init__(self, key: str, label: str, **kwargs):
        initial_error = kwargs.get('errors', [])
        if not isinstance(initial_error, list):
            initial_error = [initial_error]
        super().__init__(key, initial_error)
        self.set_props(dict(label=label, **kwargs))

    def set_props(self, props):
        self.label = props.get('label', 'Label')
        self.value = props.get('initial_value', self.empty_value)
        self.required = props.get('required', True)
        self.language = props.get('language', None)
        self.hint = props.get('hint', None)
        self.full_width = props.get('full_width', False)
        self.disabled = props.get('disabled', False)

    def render(self, ctx: dict):
        return {'type': self.type, 'key': self.key, 'label': self.label,
            'value': self.serialize_value(), 'language': self.language,
            'required': self.required, 'hint': self.hint, 'fullWidth': self
            .full_width, 'disabled': self.disabled, 'errors': self.errors}

    def serialize_value(self) ->str:
        return self.value if self.value is not None else ''
