import { PanelLabel } from "@/page/app/components/InspectPanel/components/Label";
import { SerializedStyles, css } from "@emotion/react";
import { fixedWidthStyle } from "@/page/app/components/InspectPanel/PanelSetters/style";

import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";

const applyBaseSelectWrapperStyle = (
  isSetterSingleRow: boolean = false
): SerializedStyles => {
  return isSetterSingleRow
    ? css`
        width: 100%;
      `
    : fixedWidthStyle;
};

const setterContainerStyle = (isSetterSingleRow: boolean = false) => {
  const basicStyle = css`
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  `;
  const singleRowStyle = css`
    flex-direction: column;
    gap: 8px;
  `;
  return css`
    display: flex;
    padding: 8px 0;
    ${isSetterSingleRow ? singleRowStyle : basicStyle};
    width: 100%;
  `;
};


// eslint-disable-next-line @typescript-eslint/no-explicit-any
const BaseSelectSetter = (props: any) => {
  const {
    isSetterSingleRow,
    options,
    attrName,
    handleUpdateDsl,
    value,
    onChange,
    labelName,
    labelDesc,
    labelSize,
  } = props;

  return (
    <div css={setterContainerStyle(isSetterSingleRow)}>
      <span>
        {labelName && (
          <PanelLabel
            labelName={labelName}
            labelDesc={labelDesc}
            labelSize={labelSize}
          />
        )}
      </span>
      <div css={applyBaseSelectWrapperStyle(isSetterSingleRow)}>
        <Autocomplete
          id="combo-box-demo"
          value={value}
          options={options}
          sx={{ width: 180 }}
          renderInput={(params) => (
            <TextField {...params} size="small" />
          )}
          onChange={(_event, val) => {

            const { value } = val??{value:""};

            handleUpdateDsl(attrName, value);
            onChange?.(value);
          }}
      
        />
      </div>
    </div>
  );
};

BaseSelectSetter.displayName = "BaseSelect";

export default BaseSelectSetter;
