export default [
  {
    name: 'items',
    title: '折叠面板项',
    setter: {
      componentName: 'ArraySetter',
      props: {
        itemSetter: {
          componentName: 'ObjectSetter',
          props: {
            config: {
              items: [
                {
                  name: 'title',
                  title: '名称',
                  defaultValue: '折叠面板项',
                  important: true,
                  setter: 'StringSetter',
                },
                {
                  name: 'primaryKey',
                  title: '项目编号',
                  condition: () => false,
                  setter: 'StringSetter',
                },
                {
                  name: 'icon',
                  title: '图标',
                  initialValue: '',
                  setter: 'IconSetter',
                },
              ],
            },
          },
          initialValue: () => {
            return {
              primaryKey: String(Math.floor(Math.random() * 10000)),
              title: '折叠面板项',
              icon: '',
            }
          },
        },
      },
    },
    extraProps: {
      getValue(target) {
        const map = target.node.children.map((child) => {
          const primaryKey = child.getPropValue('primaryKey')
            ? String(child.getPropValue('primaryKey'))
            : child.id
          return {
            primaryKey,
            title: child.getPropValue('title') || '折叠面板项',
            icon: '',
          }
        })
        return map
      },
      setValue(target, value) {
        const { node } = target
        const map = {}
        if (!Array.isArray(value)) {
          value = []
        }
        value.forEach((item) => {
          const tabitem = Object.assign({}, item)
          map[item.primaryKey] = tabitem
        })

        node.children.mergeChildren(
          (child) => {
            const primaryKey = String(child.getPropValue('primaryKey'))
            if (Object.hasOwnProperty.call(map, primaryKey)) {
              child.setPropValue('title', map[primaryKey].title)
              child.setPropValue('icon', map[primaryKey].icon)
              delete map[primaryKey]
              return false
            }
            return true
          },
          () => {
            const items = []
            for (const primaryKey in map) {
              if (Object.hasOwnProperty.call(map, primaryKey)) {
                items.push({
                  componentName: 'AccordionSelector.Item',
                  props: map[primaryKey],
                })
              }
            }
            return items
          },
          (child1, child2) => {
            const a = value.findIndex(
              (item) => String(item.primaryKey) === String(child1.getPropValue('primaryKey')),
            )
            const b = value.findIndex(
              (item) => String(item.primaryKey) === String(child2.getPropValue('primaryKey')),
            )
            return a - b
          },
        )
      },
    },
  },
]
