const { Cascader } = antd;

/** 保持原 PlazaCascader 对外 value/onChange 协议，内部用 antd Cascader */
function PlazaCascader({ value, onChange, placeholder = "请选择", style, allowClear = true }) {
  const cascaderValue = value && value.path && value.path.length ? value.path : undefined;

  const handleChange = (path, selectedOptions) => {
    if (!path || !path.length) {
      onChange(null);
      return;
    }
    const leaf = selectedOptions[selectedOptions.length - 1];
    onChange({
      path: path.slice(),
      label: leaf ? leaf.label : "",
      names: (selectedOptions || []).map((o) => o.label),
      plazaId: path[path.length - 1],
      isLeaf: true,
    });
  };

  return (
    <Cascader
      data-screen-label="plaza-cascader"
      options={SPData.PLAZA_TREE}
      value={cascaderValue}
      onChange={handleChange}
      placeholder={placeholder}
      allowClear={allowClear}
      expandTrigger="click"
      changeOnSelect={false}
      displayRender={(labels) => labels[labels.length - 1] || ""}
      style={{ width: "100%", ...style }}
    />
  );
}

Object.assign(window, { PlazaCascader });
